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 -- Wednesday, April 14

1. Analyzing an Expression before Evaluation

We briefly saw this idea of using analyze to cut down on the amount of work we do at run time. What does analyze do? First, recall what eval and apply do.

Consider the form of analyze:

Ok, so what's an execution procedure? Compare it to what we know about regular procedures:

Using this method, we analyze an expression only once but can evaluate it many times, with respect to different environments.

2. Code for Analyze

3. An Example

Consider evaluating the following expressions with and without using analyze.

4. Adding and to the Evaluator

Recall that and is a special form that takes an arbirary number of arguments. And evalutes each argument in turn until one of its arguments is false, in which case it returns false. For example,

Write the function and? to see if an expression is an and.

    (define (and? exp) HighlitedAnswer (and (pair? exp) (eq? (car exp) 'and)) EndOfAnswer )

Consider adding and to the Evaluator. Write a version of eval-and that does not do any desugaring.

    (define (eval-and exp env) HighlitedAnswer (define (iter clauses) (cond ((null? clauses) #t) ((true? (mc-eval (car clauses) env)) (iter (cdr clauses))) (else #f))) (iter (cdr exp)) EndOfAnswer)

Write a version of eval-and that desugars into an if-statement.

    (define (eval-and exp env) HighlitedAnswer (cond ((null? (cdr exp)) true) (else (mc-eval (list 'if (cadr exp) (cons 'and (cddr exp)) false) env))) EndOfAnswer)





next up previous
Next: About this document



Michael E. Leventon
Wed Apr 14 10:42:34 EDT 1999