Announcements

Quiz 1

Wednesday, March 19, Walker Gym (3rd floor) You bring one sheet of personal notes, we'll bring Scheme manuals and text books.

Java

Please start familiarizing yourself with the Java development environment of your choice. J++ will arrive shortly.

Mantras

  1. Every expression* has a value.
  2. To find the value of a combination,
  3. The value of a lambda expression is a procedure.
  4. The value of a quote expression is the thing quoted.

*: Except for errors, infinite loops, and the define special form.

Today's Example

(define make-adder
  (lambda (x)
    (lambda (y)
      (+ x y))))

(define add-2 (make-adder 2))
(define add-3 (make-adder 3))

(add-2 5)
(add-3 5)
(add-2 12)

Definitions

A quantifier controls names within a specified scope. The names it controls are called the bound variables of the quantifier, and we say "the variables are bound" by the quantifier within its scope.

lambda is a quantifier. Its parameters are the bound variables, and its scope is the body of the lambda expression.

Those variables that are not controlled by a quantifier are called free. Bound variables can be renamed by uniformly changing them in the bound variable list and throughout the scope of the controlling quantifier, provided that doing so doesn't change which lambda controls the uses of the name. Free variables can't be renamed in this way because there's no lambda that controls them.

Back to Our Example

Consider (lambda (x) ...).

Consider (lambda (y) ...) by itself

Consider (lambda (x) (lambda (y) ...))

Today's Example

(define make-adder
  (lambda (x)
    (lambda (y)
      (+ x y))))

(define add-2 (make-adder 2))
(define add-3 (make-adder 3))

(add-2 5)  ; ==> 7
(add-3 5)  ; ==> 8
(add-2 12) ; ==> 14

Rules of the Environment Model

Rule 1: To find the value of an expression you must have both an expression and an environment.

Rule 2: Procedures have two parts: text (composed of the procedure's parameters and its body) and an environment.

Rule 3: When you apply a procedure to arguments, be sure to use the arguments and all three parts of the procedure.

Details of the Environment Model

  1. Variable: look it up in the environment.
  2. lambda expression: create a compound procedure ("double bubble"). The text (i.e. parameters and body) come from the lambda expression; the environment is the one you are using to evaluate the lambda expression.
  3. Combination: "Evaluate, then apply". To apply a compound procedure ("double bubble"):

Today's Example

(define make-adder
  (lambda (x)
    (lambda (y)
      (+ x y))))

(define add-2 (make-adder 2))
(define add-3 (make-adder 3))

(add-2 5)  ; ==> 7
(add-3 5)  ; ==> 8
(add-2 12) ; ==> 14

Mantras, revisited

  1. Every expression1 has a value.
  2. To find the value of a combination,
  3. The value of a lambda expression is a compound procedure.3
  4. The value of a quote expression is the thing quoted.

Notes:
  1. Except for errors, infinite loops, and the define special form.
  2. To apply a compound procedure to arguments use the arguments and all three parts of the procedure (create, fill, link, evaluate).
  3. A compound procedure ("double bubble") has parameters, body, and an environment. The parameters and the body, together, are called the text.