【SICP練習】132 練習3.63,sicp練習1323.63
練習3-63原文
Exercise 3.63. Louis Reasoner asks why the sqrt-stream procedure was not written in the following more straightforward way, without the local variable guesses:
(define (sqrt-stream x) (cons-stream 1.0 (stream-map (lambda (guess) (sqrt-improve guess x)) (sqrt-stream x))))
Alyssa P. Hacker replies that this version of the procedure is considerably less efficient because it performs redundant computation. Explain Alyssa’s answer. Would the two versions still differ in efficiency if our implementation of delay used only (lambda () ) without using the optimization provided by memo-proc (section 3.5.1)?
分析
書中第233頁的sqrt-stream和Louis的相比差別在於其用了gusses作為返回結果,而其是一個流。其每一次的運算都會調用上一次的結果,僅僅是多計算一次。因為有了memo-proc,複雜度就是theta(n)。
雖然Louis也是返迴流,但是它相比於書中的定義,不是從上一次開始調用而是從n=1開始。因此對於任意的計算,其都需要n步。所以它的複雜度是theta(n^2)。
而如果去掉了sqrt-stream的memo-proc,則兩者的效果是相同的,因為其實通過guesses來維持著流以達到memo-proc的效果。而Louis的定義則沒有依靠memo-proc。