Exercise 4.2

a

eval is written so that the predicates classifying expressions are tested in a specific order. Changing the order of those tests mean either re-writing them or changing the language definition so that the current predicates happen to work (see part b.)
Louis’ proposed changes to eval test for application before assignment so:

(define (eval exp env) 
  (cond ((self-evaluating? exp) exp)
        ((variable? exp) (lookup-variable-value exp env))
        ((quoted? exp) (text-of-quotation exp))
        ((application? exp)
         (apply (eval (operator exp) env)
                (list-of-values (operands exp) env)))
        ((assignment? exp) (eval-assignment exp env))
        ((definition? exp) (eval-definition exp env))
        ((if? exp) (eval-if exp env))
        ((lambda? exp)
         (make-procedure (lambda-parameters exp)
                         (lambda-body exp)
                         env))
        ((begin? exp) 
         (eval-sequence (begin-actions exp) env))
        ((cond? exp) (eval (cond->if exp) env))
        (else
         (error "Unknown expression type -- EVAL" exp))))

Evaluate (define x 3):
(eval ‘(define x 3) env) ;; (application? ‘(define x 3)) => true
-> (apply (eval ‘define env)
(list-of-values (‘x 3) env)))
-> (eval ‘define env) ;; (variable? ‘define) => true
-> (lookup-variable-value exp env)
-> (error “Unbound variable” ‘define)

b

To force application to be evaluated before assignment the language is changed to add the symbol call to an application type expression. The application predicate and selectors need to be changed.

(define (application? exp)  
  (tagged-list? exp 'call))
(define (operator exp) (cadr exp))
(define (operands exp) (cddr exp))
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s