Exercise 4.8: “Named let” is a variant
of let that has the form
(let ⟨var⟩ ⟨bindings⟩ ⟨body⟩)
The ⟨bindings⟩ and ⟨body⟩ are just as in ordinary let,
except that ⟨var⟩ is bound within ⟨body⟩ to a procedure whose body
is ⟨body⟩ and whose parameters are the variables in the ⟨bindings⟩.
Thus, one can repeatedly execute the ⟨body⟩ by invoking the procedure
named ⟨var⟩. For example, the iterative Fibonacci procedure
(1.2.2) can be rewritten using named let as follows:
(define (fib n)
(let fib-iter ((a 1) (b 0) (count n))
(if (= count 0)
b
(fib-iter (+ a b)
a
(- count 1)))))
Modify let->combination of Exercise 4.6 to also support named
let.
练习 4.8:“命名 let” 是 let 的一种变体,其形式为:
(let ⟨var⟩ ⟨bindings⟩ ⟨body⟩)
其中 ⟨bindings⟩ 和 ⟨body⟩ 与普通 let 相同,只是 ⟨var⟩ 在 ⟨body⟩ 内被绑定到一个过程,该过程的体为 ⟨body⟩,其参数为 ⟨bindings⟩ 中的变量。因此,可以通过调用以 ⟨var⟩ 命名的过程来反复执行 ⟨body⟩。例如,迭代型 Fibonacci 过程(1.2.2 节)可以用命名 let 改写如下:
(define (fib n)
(let fib-iter ((a 1) (b 0) (count n))
(if (= count 0)
b
(fib-iter (+ a b)
a
(- count 1)))))
修改练习 4.6 中的 let->combination,使其也能支持命名 let。
(let ⟨var⟩ ⟨bindings⟩ ⟨body⟩) (define (fib n)
(let fib-iter ((a 1) (b 0) (count n))
(if (= count 0)
b
(fib-iter (+ a b)
a
(- count 1)))))