SICP 1.19 习题答案
这道题需要一些数学知识,题目中已经提示:
Show that if we apply such a transformation Tpq twice, the effect is the same as using a single transformation Tp'q' of the same form, and compute p' and q' in terms of p and q
应用两次Tpq相当于一次Tp'q',所以可以通过这样的方式进行推导: Tpq = bq+aq+ap, bp+aq 将a<-bq+aq+ap,b<-bp+aq代入上面对式子中,得 Tp'q'=(bp+aq)q+(bq+aq+ap)q+(bq+aq+ap)p, (bp+aq)p+(bq+aq+ap)q 展开后提出a和b,可得 q'= 2pq+qq p'= qq+pp
代入后试验结果确实如此。
(define (fib n)
(fib-iter 1 0 0 1 n))
(define (fib-iter a b p q count)
(cond ((= count 0) b)
((even? count)
(fib-iter a
b
(+ (* p p) (* q q))
(+ (* 2 (* p q)) (* q q))
(/ count 2)))
(else (fib-iter (+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)))))