Skip to content

Commit 42e4561

Browse files
committed
Add implementation of exercise 3-51
1 parent 98f31c8 commit 42e4561

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-
- 章节三: 50/82
28+
- 章节三: 51/82
2929
- 章节四: TODO
3030
- 章节五: TODO
3131
* 运行

chapter3/exercise3-51.rkt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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)

chapter3/stream.rkt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
(stream-filter pred (stream-cdr stream))))
6666
(else (stream-filter pred (stream-cdr stream)))))
6767

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+
6875
;; Testing utility functions
6976
(define (stream-to-list stream n)
7077
(if (or (= n 0)
@@ -165,4 +172,9 @@
165172
(check-equal? result 'done)
166173
(check-equal? results '(5 4 3 2 1))
167174
)
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+
)
168180
)

0 commit comments

Comments
 (0)