Skip to content

Commit 98f31c8

Browse files
committed
Add implementation of exercise 3-50
1 parent cfb57f8 commit 98f31c8

File tree

2 files changed

+29
-1
lines changed

2 files changed

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

chapter3/exercise3-50.rkt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#lang racket
2+
(require "stream.rkt")
3+
4+
(define (stream-map proc . argstreams)
5+
(if (stream-null? (car argstreams))
6+
the-empty-stream
7+
(cons-stream
8+
(apply proc (map stream-car argstreams))
9+
(apply stream-map
10+
(cons proc (map stream-cdr argstreams))))))
11+
12+
(module+ test
13+
(require rackunit)
14+
15+
(test-case "Test for two streams mapping"
16+
(define s1 (list-to-stream '(1 2 3 4)))
17+
(define s2 (list-to-stream '(10 20 30 40)))
18+
(define added (stream-map + s1 s2))
19+
(check-equal? (stream-to-list added 4) '(11 22 33 44))
20+
)
21+
22+
(test-case "Test for different length streams(stop at shortest)"
23+
(define s1 (list-to-stream '(1 2)))
24+
(define s2 (list-to-stream '(10 20 30 40)))
25+
(define added (stream-map + s1 s2))
26+
(check-equal? (stream-to-list added 4) '(11 22))
27+
)
28+
)

0 commit comments

Comments
 (0)