Announcements
Quiz 1
Wednesday, March 19, Walker Gym (3rd floor)
- 5pm to 7pm (you may not leave early)
- 7pm to 9pm
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
- Every expression* has a value.
- To find the value of a combination,
- Find the values of the subexpressions, in any order
- Apply the value of the first to the values of the rest
- The value of a lambda expression is a procedure.
- 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) ...).
- It binds the variable x.
- The scope of x is the body, ...
Consider (lambda (y) ...) by itself
- It binds the variable y.
- The scope of y is the body, ...
Consider (lambda (x) (lambda (y) ...))
- Inside of ... both x and y are bound.
- x is bound by the outer lambda
- y is bound by the inner lambda
- + is free
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
- Variable: look it up in the environment.
- 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.
- Combination: "Evaluate, then apply". To
apply a compound procedure ("double bubble"):
- Create a new frame [parameters].
- Fill the frame [arguments].
- Link the frame to make it an environment [environment].
- Evaluate the body using the new environment [body].
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
- Every expression1 has a value.
- To find the value of a combination,
- Find the values of the subexpressions, in any order.
- Apply2 the value of the first to the values of the
rest.
- The value of a lambda expression is a compound procedure.3
- The value of a quote expression is the thing quoted.
Notes:
- Except for errors, infinite loops, and the define special
form.
- To apply a compound procedure to arguments use the arguments and
all three parts of the procedure (create, fill, link, evaluate).
- A compound procedure ("double bubble") has parameters,
body, and an environment. The parameters and the body, together, are
called the text.