亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 15727 | 回復(fù): 56
打印 上一主題 下一主題

SICP 習題參考答案,3.5 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-09-02 15:50 |只看該作者 |倒序瀏覽

  1. 本貼用與存放 SICP 3.5 節(jié)的習題參考答案。為保持可讀性,請勿直接回復(fù)本貼。討論請另開新貼。
復(fù)制代碼




[ 本帖最后由 win_hate 于 2008-9-13 16:59 編輯 ]

論壇徽章:
0
2 [報告]
發(fā)表于 2008-09-02 15:52 |只看該作者

Exercise 3.50.

Exercise 3.50.  Complete the following definition, which generalizes stream-map to allow procedures that take multiple arguments, analogous to map in section 2.2.3, footnote 12.


  1. (define (stream-map proc . argstreams)
  2.   (if (<??> (car argstreams))
  3.       the-empty-stream
  4.       (<??>
  5.        (apply proc (map <??> argstreams))
  6.        (apply stream-map
  7.               (cons proc (map <??> argstreams))))))
復(fù)制代碼

[ 本帖最后由 win_hate 于 2008-9-2 15:54 編輯 ]

論壇徽章:
0
3 [報告]
發(fā)表于 2008-09-02 15:53 |只看該作者

Exercise 3.50. 答案


  1. (define (stream-map proc . argstreams)
  2.   (if (stream-null? (car argstreams))
  3.       the-empty-stream
  4.       (cons-stream
  5.         (apply proc (map stream-car argstreams))
  6.         (apply stream-map
  7.                (cons proc (map stream-cdr argstreams))))))
復(fù)制代碼

論壇徽章:
0
4 [報告]
發(fā)表于 2008-09-02 15:54 |只看該作者

Exercise 3.51.

Exercise 3.51.  In order to take a closer look at delayed evaluation, we will use the following procedure, which simply returns its argument after printing it:

(define (show x)
  (display-line x)
  x)

What does the interpreter print in response to evaluating each expression in the following sequence?59

(define x (stream-map show (stream-enumerate-interval 0 10)))
(stream-ref x 5)
(stream-ref x 7)

論壇徽章:
0
5 [報告]
發(fā)表于 2008-09-02 15:56 |只看該作者

Exercise 3.51 答案


  1. 0       ;;<- define
  2. 1       ;;<- ref 5
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6       ;;<- ref 7
  8. 7
復(fù)制代碼


本題想說,force 有記憶。 show 有輸出的副作用,但如果只提取已有的值,則無需執(zhí)行 show.

[ 本帖最后由 win_hate 于 2009-4-15 15:36 編輯 ]

論壇徽章:
0
6 [報告]
發(fā)表于 2008-09-02 15:57 |只看該作者

Exercise 3.52

Exercise 3.52.  Consider the sequence of expressions


  1. (define sum 0)
  2. (define (accum x)
  3.   (set! sum (+ x sum))
  4.   sum)
  5. (define seq (stream-map accum (stream-enumerate-interval 1 20)))
  6. (define y (stream-filter even? seq))
  7. (define z (stream-filter (lambda (x) (= (remainder x 5) 0))
  8.                          seq))
  9. (stream-ref y 7)
  10. (display-stream z)
復(fù)制代碼


What is the value of sum after each of the above expressions is evaluated? What is the printed response to evaluating the stream-ref and display-stream expressions? Would these responses differ if we had implemented (delay <exp>) simply as (lambda () <exp>) without using the optimization provided by memo-proc ? Explain.

[ 本帖最后由 win_hate 于 2008-9-2 16:00 編輯 ]

論壇徽章:
0
7 [報告]
發(fā)表于 2008-09-02 15:59 |只看該作者

Exercise 3.52. 答案

[解]

  1. (define seq (stream-map accum (stream-enumerate-interval 1 20)))
復(fù)制代碼


此語句執(zhí)行后,sum=1

  1. (define y (stream-filter even? seq))
復(fù)制代碼


此語句執(zhí)行后, sum=1+2+3=6

  1. (define z (stream-filter (lambda (x) (= (remainder x 5) 0))
  2.                          seq))
復(fù)制代碼


此語句執(zhí)行后, sum=6+4=10


  1. (stream-ref y 7)
復(fù)制代碼


此語句執(zhí)行時,seq 前 4 項已知,后面的要動態(tài)計算,取其中第 8 個偶數(shù)。seq 的第 n 項為正整數(shù)列的前 n 項之和 n(n+1)/2,為偶數(shù)僅當 n=3,0 (mod 4)。所以 n=3, 4, 7, 8, 11, 12, 15, 16.

(stream-ref y 7) 的值為 16(16+1)/2=136. 如果在交互環(huán)境,輸出 135,如果是腳本運行模式,沒有輸出。


  1. (display-stream z)
復(fù)制代碼


此語句輸出 seq 中的全部能被 5 整除的數(shù)。n(n+1)/2 為 5 的倍數(shù),當且僅當 n=4,0 (mod 5)。所以 n 取 4,5,9,10,14,15,19,20. 由于

(map (lambda (n) (/ (* n (+ n 1)) 2)) '(4 5 9 10 14 15 19 20))

得到

(10 15 45 55 105 120 190 210)

所以輸出為:

10
15
45
55
105
120
190
210


========================================


如果沒有使用 memo-proc 優(yōu)化,則 force 不保存首次求值的結(jié)果。

(1) define seq, sum=1

(2) define y, sum=6

(3) define z,

由于 seq 的第一項不是 promise,所以不產(chǎn)生累加 sum 的副作用。

sum=6+2+3+4=15

(4) (stream-ref y 7)

從流 y 中提第 8 個數(shù),即用 stream-filter 在 seq 中提取第 8 個偶數(shù)。此時 seq 的通項為

s(n)=15+n(n+1)/2-6,其中 n>=4

s(n) 為偶數(shù),相當于  n(n+1)/2=1 (mod 4),化簡有 (n-1)(n-2)=0 (mod 4),即 n=1,2 (mod 4)

這樣的 n 依次為 5, 6, 9, 10, 13, 14, 17. 所以 (stream-ref y 7) 的值,也就是 sum 的當前值為 s(17)=162

(5) (display-steam z)

此表達式遍歷流 z,而 z 是 stream-filter 過濾 seq, 提取 5 的倍數(shù)得到的列表。

此時 seq 的通項為:

t(n)=162+n(n+1)/2-10,其中 4 < n < 21

5|t(n),相當于 n(n+1)/2=3 (mod 5),化簡有 (n-2)^2=0 (5).
即 n=2 (mod 5)

這樣的 n 依次為 7, 12 17。所以該語句的輸出為:15, t(7), t(12), t(17), 即

15
180
230
305

而 sum 的當前值為 t(20)=362.

[ 本帖最后由 win_hate 于 2008-9-2 23:36 編輯 ]

論壇徽章:
0
8 [報告]
發(fā)表于 2008-09-03 11:56 |只看該作者

Exercise 3.53.

Exercise 3.53.  Without running the program, describe the elements of the stream defined by

(define s (cons-stream 1 (add-streams s s)))

論壇徽章:
0
9 [報告]
發(fā)表于 2008-09-03 11:57 |只看該作者

Exercise 3.53 答案

每一項是前一項的兩倍,得到序列 {2^n, n in  N}.

[ 本帖最后由 win_hate 于 2008-9-3 12:57 編輯 ]

論壇徽章:
0
10 [報告]
發(fā)表于 2008-09-03 11:58 |只看該作者

Exercise 3.54

Exercise 3.54.  Define a procedure mul-streams, analogous to add-streams, that produces the elementwise product of its two input streams. Use this together with the stream of integers to complete the following definition of the stream whose nth element (counting from 0) is n + 1 factorial:

(define factorials (cons-stream 1 (mul-streams <??> <??>)))
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP