File tree Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 25
25
习题完成情况:
26
26
- 章节一: 43/46
27
27
- 章节二: 88/97
28
- - 章节三: 50 /82
28
+ - 章节三: 51 /82
29
29
- 章节四: TODO
30
30
- 章节五: TODO
31
31
* 运行
Original file line number Diff line number Diff line change
1
+ #lang racket
2
+ (require "stream.rkt " )
3
+
4
+ (define (show x)
5
+ (display-line x)
6
+ x)
7
+
8
+
9
+ ;;; 输出 0, 因为 (stream-enumerate-interal 0 10) 会生成一组 0 - 10 的
10
+ ;;; 流, 而 stream-map 会把第一个流应用在 show 函数, 延时未执行的流是
11
+ ;;; 1 - 10
12
+ (define x (stream-map show (stream-enumerate-interal 0 10 )))
13
+
14
+ ;;; 输出 1 2 3 4 5,因为 stream-ref 需要递归访问 0 - 5, 因为 0 已经被
15
+ ;;; 处理(memo-proc), 所以只会输出 1 - 5
16
+ ;;; 返回 5, show 函数不只打印,还会返回值,而5是最后一个值,所以也被返
17
+ ;;; 回了.
18
+ (stream-ref x 5 )
19
+
20
+ ;;; 输出 6 7, 因为 stream-ref 需要递归访问 0 - 7, 而 0-5 都已经被计算
21
+ ;;; 了,所以不会被打印,只会输出 6 - 7
22
+ ;;; 返回 7
23
+ (stream-ref x 7 )
Original file line number Diff line number Diff line change 65
65
(stream-filter pred (stream-cdr stream ))))
66
66
(else (stream-filter pred (stream-cdr stream )))))
67
67
68
+ (define (stream-enumerate-interal low high)
69
+ (if (> low high)
70
+ the-empty-stream
71
+ (cons-stream
72
+ low
73
+ (stream-enumerate-interal (+ low 1 ) high))))
74
+
68
75
;; Testing utility functions
69
76
(define (stream-to-list stream n)
70
77
(if (or (= n 0 )
165
172
(check-equal? result 'done )
166
173
(check-equal? results '(5 4 3 2 1 ))
167
174
)
175
+
176
+ (test-case "Test for stream-enumerate-interal "
177
+ (define s (stream-enumerate-interal 5 10 ))
178
+ (check-equal? (stream-to-list s 10 ) '(5 6 7 8 9 10 ))
179
+ )
168
180
)
You can’t perform that action at this time.
0 commit comments