SICP 全笔记

Exercise 4.1. Notice that we cannot tell whether the metacircular evaluator evaluates operands from left to right or from right to left. Its evaluation order is inherited from the underlying Lisp: If the arguments to cons in list-of-values are evaluated from left to right, then list-of-values will evaluate operands from left to right; and if the arguments to cons are evaluated from right to left, then list-of-values will evaluate operands from right to left.

Write a version of list-of-values that evaluates operands from left to right regardless of the order of evaluation in the underlying Lisp. Also write a version of list-of-values that evaluates operands from right to left.

现在的 scheme 实现中,不确定的是在使用过程的时候,对参数的求值的自左向右还是自右向左。所以我们不要把这两个求值放到过程参数的位置。在传入之前先求值就可以避免这种不确定性。

;; left to right
(define (list-of-values exps env)
  (if (no-operands? exps)
      '()
      (let ((v (eval (first-operand exps) env)))
        (cons v (list-of-values (rest-operands exps) env)))))

;; right to left
(define (list-of-values exps env)
  (if (no-operands? exps)
      '()
      (let ((v-rest (list-of-values (rest-operands exps) env))
            (v (eval (first-operand exps) env)))
        (cons v v-rest))))