Skip to content

Week 11 Notes

  • Reading Ch. 3.1: Introduction (02/08/22)
    • Text → Interpreter → Computation
    • An interpreter is a program that carries out evaluation and execution procedures
    • Many interpreters have an elegant common structure: two mutually recursive functions
      • The first evaluates expressions in environments
      • The second applies functions to arguments
  • Reading Ch. 3.2: Functional Programming (02/08/22)
    • Scheme programs consist of expressions, which are either call expressions or special forms
      • Call expressions: (<operator> <operand 1> <operand 2>…)
      • Special forms
        • (if <predicate> <consequent> <alternative>)
        • (and <e1> … <en>)
        • (or <e1> … <en>)
        • (not <e>)
        • (define <name> <value>)
        • (define (<name> <formal parameters>) <body>)
        • (lambda (<formal-parameters>) <body>)
        • (begin <ex 1> <ex2> …) → Execute multiple sub-expressions
      • All of the above have the same evaluation procedures similar to those in Python
      • Scheme supports local definitions with the same lexical scoping rules as Python
    • Boolean values: #t, #f
    • All values in this dialect of Scheme are immutable
    • Compound values
      • pairs are created with cons built-in function, elements are accessed with car and cdr
      • Recursive lists are also built-in
        • nil or ’() represents an empty list
        • list function, null? predicate
      • Scheme lists have a linked list structure with elements accessed by car and cdr, but display all elements
    • Symbolic data
      • Refer to the symbols rather than the values they hold by preceding them with a single quotation mark
  • Lecture 25: Scheme (02/08/22)
  • Lab 09: Scheme, Scheme Lists (03/08/22)
  • Discussion 09: Scheme (03/08/22)
  • Reading Ch. 3.3: Exceptions (03/08/22)
    • There is no single correct approach to handling errors in a program
      • Programs designed to provide persistent service like a web server should be robust to errors, logging them for later consideration but continuing to service new requests as long as possible
      • Python interpreter terminates immediately and prints an error message
    • Raising an exception
      • Interrupt the program’s normal flow of execution
      • Signal that some exceptional circumstance has arisen
      • Unless the exception is handled, the interpreter will return directly to an enclosing part of the program that was designated to react to that circumstance (in most cases, the interactive read-eval-print loop)
      • The interpreter prints a stack backtrace, a structured block of text that describes the nested set of active function calls in the branch of execution where the exception was raised
    • An exception is a object instance with a class that inherits from the BaseException class → Constructs an exception instance and raises it
    • Handing exceptions - try:

      <try suite>

      except <exception class> as <name>:

      <except suite>

  • <try suite> executes
  • <except suite> only executes when an exception is raised during <try suite> execution
  • Each except clause (there can be multiple) specifies the particular class of exception to handle
  • If a suitable exception is raised, it will be handled by the following <except suite> with <name> bound to the raised exception object
  • Exception objects can have attributes. User-defined exception classes can have additional attributes
  • Reading Ch. 3.4: Interpreters for Languages with Combination (04, 07/08/22)
    • Metalinguistic abstraction: establishing new languages in terms of other languages
    • Interpreter: a function that when applied to an expression of the language performs the actions required to evaluate that expression
    • Expression trees: represent code in a tree-like data structure, where each node is an expression
    • To write an interpreter, we must operate on expressions as data → expression trees
    • Parsing: the process of generating expression trees from raw text input
      • A parser consists of a lexical analyzer and a syntactic analyzer
      • The lexical analyzer (tokenizer) partitions the input string into tokens, minimal syntactic units of the language such as names and symbols. Lexical analysis is an iterative process
      • The syntactic analyzer constructs an expression tree from this sequence of tokens. Syntactic analysis is a tree-recursive process
    • Read-eval-print loops (REPL): A mode of interacting that reads an expression, evaluates it, and prints the result for the user
      • To generalize the interpreter, REPL is parameterized by a parsing function, an evaluation function, and exception types handled by the try statement
  • Lecture 26: Exceptions (04/08/22)
  • Lecture 27: Calculator (07/08/22)
    • Machine languages: statements interpreted by the hardware itself
      • A fixed set of instructions invoke operations implemented by the circuitry of the CPU
      • Very hard to program, operations refer to specific hardware memory addresses, and no abstraction mechanisms
    • High-level languages: statements & expressions are interpreted by another program or compiled (translated) into another language
      • Provide means of abstractions such as naming, function definition, and objects
      • Very common for high-level languages to be built on top of other high-level languages. An interpreter for C may be written in machine languages and C can be used to build the interpreter for Python
      • Abstract away system details to be independent of hardware and operating systems
      • Python is compiled in to Python byte code (a lower-level language) and an interpreter runs the byte code
    • Metalinguistic abstraction: a powerful form of abstraction - defining a new language that is tailored to a particular type of application or problem domain (Erlang: concurrent programs (chat servers, ATMs), MediaWiki markup language (Wikipedia),…)
      • Syntax: the legal statements and expressions in the language
      • Semantics: the execution/evaluation rule for those statements and expressions
    • To create a new language
      • Specification: A document describing the syntax and semantics of the language, or
      • Canonical Implementation: An interpreter or compiler for the language
    • Text —(Lexical analysis)→ Tokens —(Syntactic analysis)→ Expression
  • Homework 07 (07/08/22)