Exercise 3.9: In 1.2.1 we used the
substitution model to analyze two procedures for computing factorials, a
recursive version
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
and an iterative version
(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product
counter
max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
Show the environment structures created by evaluating (factorial 6)
using each version of the factorial procedure.
练习 3.9:在 1.2.1 中,我们用代换模型分析了两个计算阶乘的过程,一个是递归版本
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
另一个是迭代版本
(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product
counter
max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
请画出用两个版本的 factorial 过程分别对 (factorial 6) 求值时所创建的环境结构。
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1))))) (define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product
counter
max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))