Skip to content

Commit 6bc0856

Browse files
committed
Add implementation of exercise 3-52
1 parent 42e4561 commit 6bc0856

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
习题完成情况:
2626
- 章节一: 43/46
2727
- 章节二: 88/97
28-
- 章节三: 51/82
28+
- 章节三: 52/82
2929
- 章节四: TODO
3030
- 章节五: TODO
3131
* 运行

chapter3/exercise3-51.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
;;; 了,所以不会被打印,只会输出 6 - 7
2222
;;; 返回 7
2323
(stream-ref x 7)
24+
25+
;;; 明确输出和返回值的差别

chapter3/exercise3-52.rkt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#lang racket
2+
(require "stream.rkt")
3+
4+
(define sum 0)
5+
(define (accum x)
6+
(set! sum (+ x sum))
7+
sum)
8+
9+
;;; 返回1-20的流:
10+
;;; 返回一个 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171
11+
;;; 190 210 的流, 因为 string-enumerate-interal 会生成 1-20 的流, 然后
12+
;;; accum 会累加前面的流, 然后再生成一个新的流
13+
(define seq (stream-map accum (stream-enumerate-interal 1 20)))
14+
15+
;;; 返回 6, 10, 28, 36, 66, 78, 120, 136, 190, 210 的流
16+
(define y (stream-filter even? seq))
17+
18+
;;; 返回 10,15,45,55,105,120,190,210 的流
19+
(define z (stream-filter (lambda (x)
20+
(= (remainder x 5)
21+
0))
22+
seq))
23+
;;; 返回 136
24+
(stream-ref y 7)
25+
26+
;;; 输出 10,15,45,55,105,120,190,210
27+
(display-stream z)
28+
29+
;;; 如果我们简单地将 (delay <expr>) 实现成 (lambda <expr>),
30+
;;; 而不使用 memo-proc 提供的优化,响应的结果会是一样的,但是存在非常
31+
;;; 多无效的重复计算。
32+
;;; memo-proc 的作用就是确保已经计算过的结果,不会重复计算,但对计算结
33+
;;; 果没有影响

0 commit comments

Comments
 (0)