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
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.
(define (eval exp env) ((analyze exp) env))
(define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((variable? exp) (analyze-variable exp)) ((definition? exp) (analyze-definition exp)) ((lambda? exp) (analyze-lambda exp)) ((cond? exp) (analyze (cond->if exp))) ((application? exp) (analyze-application exp))))
(define (analyze-self-evaluating exp) (lambda (env) exp))
(define (analyze-variable exp) (lambda (env) (lookup-variable-value exp env)))
(define (analyze-lambda exp) (let ((bproc (analyze (lambda-body exp)))) (lambda (env) (make-procedure (lambda-parameters exp) bproc env))))
Consider evaluating the following expressions with and without using analyze.
(define foo (lambda () (+ 1 2)))
(foo)
(foo)
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,
(and) ==> #t (and #t) ==> #t (and #t #f (/ 1 0)) ==> #f
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)