内容 5.5 Compilation · 458
练习 自检推理
Exercise 5.34: Compile the iterative factorial
procedure
(define (factorial n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1))
Annotate the resulting code, showing the essential difference between the code
for iterative and recursive versions of factorial that makes one process
build up stack space and the other run in constant stack space.
练习 5.34:编译下面这个迭代型阶乘过程:
(define (factorial n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1))
对所得代码加以注释,说明迭代版本与递归版本的阶乘代码之间的本质差异——正是这一差异使得一个计算过程会不断累积栈空间,而另一个则在常数栈空间内运行。
SICP source code scheme
(define (factorial n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1)) 我的笔记 自动保存