【SICP練習】132 練習3.63,sicp練習1323.63

來源:互聯網
上載者:User

【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。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.