Skip to content

Commit 79c2492

Browse files
committed
Add implementation of exercise 3-67
1 parent 349e2b5 commit 79c2492

File tree

3 files changed

+71
-2
lines changed

3 files changed

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

chapter3/exercise3-67.rkt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#lang racket
2+
(require "stream.rkt")
3+
(require "infinite-stream.rkt")
4+
5+
;;; 需要混合另一个流是: (S1,T0), (S2, T0), (S3, T0), ..
6+
7+
;; (stream-map (lambda (x) (list (stream-car s) x))
8+
;; (stream-cdr t))
9+
;;; 是生成 (S0, T1), (S0, T2), ..
10+
11+
;; 那么 (S1,T0), (S2, T0), (S3, T0) 就是
12+
;; (stream-map (lambda (x) (list x (stream-car t)))
13+
;; (stream-cdr s))
14+
15+
(define (full-pairs s t)
16+
(if (or (stream-null? s) (stream-null? t))
17+
the-empty-stream
18+
(cons-stream
19+
;; (S0,T0)
20+
(list (stream-car s) (stream-car t))
21+
(interleave (interleave
22+
;; (S0, T1), (S0, T2), ..
23+
(stream-map (lambda (x) (list (stream-car s) x))
24+
(stream-cdr t))
25+
;; (S1,T0), (S2, T0), (S3, T0)
26+
(stream-map (lambda (x) (list x (stream-car t)))
27+
(stream-cdr s)))
28+
;; (S1,T1), (S1, T2), (S2, T2) ...
29+
(full-pairs (stream-cdr s) (stream-cdr t))
30+
))))
31+
32+
(module+ test
33+
(require rackunit)
34+
35+
(test-case "Test for full-pairs"
36+
(define s1 (list-to-stream '(1 2 3)))
37+
(define s2 (list-to-stream '(1 2 3)))
38+
(define int-pairs (stream-to-list (full-pairs s1 s2) 9))
39+
(check-equal? int-pairs '((1 1) (1 2) (2 2) (2 1) (2 3) (1 3) (3 3) (3 1) (3 2)))
40+
)
41+
)

chapter3/infinite-stream.rkt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@
2929
(define (scale-stream stream factor)
3030
(stream-map (lambda (x) (* x factor)) stream))
3131

32-
(provide add-streams scale-stream ones integers fibs)
32+
(define (interleave s1 s2)
33+
(if (stream-null? s1)
34+
s2
35+
(cons-stream (stream-car s1)
36+
(interleave s2 (stream-cdr s1)))))
37+
38+
(define (pairs s t)
39+
(if (or (stream-null? s) (stream-null? t))
40+
the-empty-stream
41+
(cons-stream
42+
(list (stream-car s) (stream-car t))
43+
(interleave
44+
(stream-map (lambda (x) (list (stream-car s) x))
45+
(stream-cdr t))
46+
(pairs (stream-cdr s) (stream-cdr t))))))
47+
48+
(provide add-streams scale-stream ones integers fibs interleave pairs)
3349

3450
(module+ test
3551
(require rackunit)
@@ -48,4 +64,16 @@
4864
(test-case "Test for infinite stream"
4965
(check-equal? (stream-ref fibs 6) 8)
5066
)
67+
(test-case "Test for interleave"
68+
(define s1 (list-to-stream '(1 2 3 4 5)))
69+
(define s2 (list-to-stream '(6 7 8 9 10)))
70+
(check-equal? (stream-to-list (interleave s1 s2) 10) '(1 6 2 7 3 8 4 9 5 10))
71+
)
72+
73+
(test-case "Test for pairs"
74+
(define s1 (list-to-stream '(1 2 3)))
75+
(define s2 (list-to-stream '(1 2 3)))
76+
(define int-pairs (stream-to-list (pairs s1 s2) 4))
77+
(check-equal? int-pairs '((1 1) (1 2) (2 2) (1 3)))
78+
)
5179
)

0 commit comments

Comments
 (0)