next up previous
Next: About this document

MASSACHVSETTS INSTITVTE OF TECHNOLOGY
Department of Electrical Engineering and Computer Science
6.001---Structure and Interpretation of Computer Programs
Spring Semester, 1999

Recitation -- Friday, March 19

1. Environment Diagrams

Why are we doing this?

What are environment diagrams made of?

What are the rules again?

  1. Combination

    To evaluate a comibination with respect to an environment, first evaluate the subexpressions with respect to the environment and then apply the value of the operator subexpression to the values of the operand subexpressions.

  2. Looking up an identifier

    Look for a value in the current frame.

    If there is no value for that identifier in the current frame, follow the link from the current frame to the one that it is linked from.

    Continue in this manner until we find a binding for the identifier we're looking up or until we run out of frames in our chain of links (i.e. we come to the global environment). If there's no binding in the global environment, the identifier we're looking up is an unbound variable.

  3. Lambda

    Evaluating a lambda expression will result in a two-part procedure object (two circles next to each other -- the double bubble).

    The pointer of the left circle points down to a list of the parameters and the body of the procedure.

    The pointer of the right circle points up to the environment frame in which the lambda expression was evaluated. Note that nothing else happens until we apply the procedure.

  4. Define

    Define adds a binding to the frame in which it is evaluated.

  5. Applying a procedure

    Draw a new environment frame. Bind the parameters to their values in this new environment frame.

    Link the new environment frame to the environment frame pointed to by the right circle of the procedure object.

    Evaluate the body of the procedure in this new environment frame.

  6. Set! (set! var expression)

    Evaluate the expression with respect to the current environment.

    Lookup the identifier in the current environment (see step #2)

    Rebind the identifier in the frame it was found to the value of the expression.

  7. Let

    Just de-sugar! (See examples, below)

2. A Simple Example

Evaluate the expressions below, following the rules of the environment model. Draw the cooresponding environment diagram in the box.

3. Let

We haven't defined the rule for let. Well, let's just desugar it:

Let just creates another frame linked from the current frame. The bindings in that frame are let-variables bound to the result of evaluating the let-expressions with respect to the original environment.

4. Another Example

Consider the example below using higher order procedures. Use the environment model to evaluate the following expressions.

4. More Environment Diagrams





next up previous
Next: About this document



Michael E. Leventon
Tue Mar 23 10:01:09 EST 1999