Practice 4.2
a
修改后的程序
(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))))
因为书中application?的定义是不属于它之前的各种表达式类型的任意复合表达式才满足application?子句,提前它的顺序的话,就不满足书中的定义了。会把所有的非自求值变量和引用表达式,都当成过程 比如在求值 (define x 3)的时候,按修改后的顺序直接满足application?子句,会执行下面的过程
(apply (eval (operator exp) env)
(list-of-values (operands exp) env))
operator和operands的定义如下
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
执行如下过程:
(apply (eval define env)
(list-of-values '(x 3) env))
执行define这个过程 ,参数是’(x 3),然而此时在被求值的环境中,没有define的约束,所以会出错
b
只需要修改application?和operator operands的定义
(define (application? exp)
(tagged-list? exp 'call))
(define (operator exp) (cadr exp))
(define (operands exp) (cddr exp))