灯下 登录
计算机科学 / SICP / 4.1.6 Internal Definitions

Exercise 4.19 · 习题

Exercise 4.19: Ben Bitdiddle, Alyssa P. Hacker,
and Eva Lu Ator are arguing about the desired result of evaluating the
expression

(let ((a 1))
(define (f x)
(define b (+ a x))
(define a 5)
(+ a b))
(f 10))

Ben asserts that the result should be obtained using the sequential rule for

define: b is defined to be 11, then a is defined to be 5,

so the result is 16. Alyssa objects that mutual recursion requires the

simultaneous scope rule for internal procedure definitions, and that it is

unreasonable to treat procedure names differently from other names. Thus, she

argues for the mechanism implemented in Exercise 4.16. This would lead

to a being unassigned at the time that the value for b is to be

computed. Hence, in Alyssa’s view the procedure should produce an error. Eva

has a third opinion. She says that if the definitions of a and b

are truly meant to be simultaneous, then the value 5 for a should be

used in evaluating b. Hence, in Eva’s view a should be 5,

b should be 15, and the result should be 20. Which (if any) of these

viewpoints do you support? Can you devise a way to implement internal

definitions so that they behave as Eva prefers?

练习 4.19:Ben Bitdiddle、Alyssa P. Hacker 和 Eva Lu Ator 正在争论对以下表达式求值的预期结果:

(let ((a 1))
(define (f x)
(define b (+ a x))
(define a 5)
(+ a b))
(f 10))

Ben 断言,结果应按 define 的顺序规则得出:b 被定义为 11,然后 a 被定义为 5,因此结果为 16。Alyssa 反对说,互递归要求内部过程定义采用同时作用域规则,而对过程名与其他名字区别对待是不合理的。因此,她主张采用练习 4.16 中实现的机制。这将导致在计算 b 的值时 a 尚未赋值,因此在 Alyssa 看来该过程应产生一个错误。Eva 则持第三种意见。她说,如果 a 和 b 的定义确实是同时的,那么在求值 b 时应使用 a 的值 5。因此在 Eva 看来,a 应为 5,b 应为 15,结果应为 20。你支持哪种观点(如果有的话)?你能设计一种实现内部定义的方式,使其行为符合 Eva 的偏好吗?

Racket #lang sicp
(let ((a 1))
 (define (f x)
 (define b (+ a x))
 (define a 5)
 (+ a b))
 (f 10))