SICP 全笔记

Exercise 4.2. Louis Reasoner plans to reorder the cond clauses in eval so that the clause for procedure applications appears before the clause for assignments. He argues that this will make the interpreter more efficient: Since programs usually contain more applications than assignments, definitions, and so on, his modified eval will usually check fewer clauses than the original eval before identifying the type of an expression.

a. What is wrong with Louis’s plan? (Hint: What will Louis’s evaluator do with the expression (define x 3)?)

b. Louis is upset that his plan didn’t work. He is willing to go to any lengths to make his evaluator recognize procedure applications before it checks for most other kinds of expressions. Help him by changing the syntax of the evaluated language so that procedure applications start with call. For example, instead of (factorial 3) we will now have to write (call factorial 3) and instead of (+ 1 2) we will have to write (call + 1 2).

题目 a

Louis 的问题在于没有搞清楚 application? 过程是如何实现的。事实上,没有任何一个判断可以判断当前的一个语句是否是 application。所以 application 满足的条件是

  1. 不是其他的 special form,比如 quote, assignment, definition, if, lambda 等等
  2. 是一个 pair。

所以 application? 只能被放到 eval 的最末尾。

题目 b

要进行显示的过程调用,我们只需要修改下面几个过程就可以

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