File tree Expand file tree Collapse file tree 10 files changed +22
-22
lines changed Expand file tree Collapse file tree 10 files changed +22
-22
lines changed Original file line number Diff line number Diff line change 18
18
(define s1 (list-to-stream '(1 2 3 4 )))
19
19
(define s2 (list-to-stream '(10 20 30 40 )))
20
20
(define added (stream-map + s1 s2))
21
- (check-equal? (stream-to-list added 4 ) '(11 22 33 44 ))
21
+ (check-equal? (stream-take-n added 4 ) '(11 22 33 44 ))
22
22
)
23
23
24
24
(test-case "Test for different length streams(stop at shortest) "
25
25
(define s1 (list-to-stream '(1 2 )))
26
26
(define s2 (list-to-stream '(10 20 30 40 )))
27
27
(define added (stream-map + s1 s2))
28
- (check-equal? (stream-to-list added 4 ) '(11 22 ))
28
+ (check-equal? (stream-take-n added 4 ) '(11 22 ))
29
29
)
30
30
)
Original file line number Diff line number Diff line change 16
16
(define s2 (list-to-stream '(3 4 5 6 )))
17
17
(define s3 (mul-streams s1 s2))
18
18
(check-equal? (stream-ref s3 2 ) 20 )
19
- (check-equal? (stream-to-list s3 4 ) '(6 12 20 30 ))
19
+ (check-equal? (stream-take-n s3 4 ) '(6 12 20 30 ))
20
20
)
21
21
(test-case "Test for factorials "
22
- (check-equal? (stream-to-list factorials 5 ) '(1 2 6 24 120 ))
22
+ (check-equal? (stream-take-n factorials 5 ) '(1 2 6 24 120 ))
23
23
;; n = 0, n+1=1 的阶乘是1
24
24
;; n = 1, n+1=2 的阶乘是2
25
25
;; n = 2, n+1=3 的阶乘是6
Original file line number Diff line number Diff line change 11
11
12
12
(test-case "Test for partial-sums "
13
13
(define s1 (partial-sums integers))
14
- (check-equal? (stream-to-list s1 5 ) '(1 3 6 10 15 ))
14
+ (check-equal? (stream-take-n s1 5 ) '(1 3 6 10 15 ))
15
15
)
16
16
)
Original file line number Diff line number Diff line change 27
27
(define s1 (list-to-stream '(1 2 3 5 7 )))
28
28
(define s2 (list-to-stream '(2 4 5 8 9 )))
29
29
(define s3 (merge s1 s2))
30
- (check-equal? (stream-to-list s3 10 ) '(1 2 3 4 5 7 8 9 ))
30
+ (check-equal? (stream-take-n s3 10 ) '(1 2 3 4 5 7 8 9 ))
31
31
)
32
32
(test-case "Test for S "
33
- (check-equal? (stream-to-list S 20 ) '(1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 ))
33
+ (check-equal? (stream-take-n S 20 ) '(1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 ))
34
34
)
35
35
)
Original file line number Diff line number Diff line change 13
13
(test-case "Test for expand "
14
14
(define x (expand 3 8 10 ))
15
15
;; 3/8 = 0.375 (有限小数)
16
- (check-equal? (stream-to-list x 10 ) '(3 7 5 0 0 0 0 0 0 0 ))
16
+ (check-equal? (stream-take-n x 10 ) '(3 7 5 0 0 0 0 0 0 0 ))
17
17
18
18
(define s (expand 1 7 10 ))
19
19
;; (expand 1 7 10) 对应 1/7 = 0.142857142857... (循环小数)
20
- (check-equal? (stream-to-list s 10 ) '(1 4 2 8 5 7 1 4 2 8 ))))
20
+ (check-equal? (stream-take-n s 10 ) '(1 4 2 8 5 7 1 4 2 8 ))))
Original file line number Diff line number Diff line change 35
35
(test-case "Test for full-pairs "
36
36
(define s1 (list-to-stream '(1 2 3 )))
37
37
(define s2 (list-to-stream '(1 2 3 )))
38
- (define int-pairs (stream-to-list (full-pairs s1 s2) 9 ))
38
+ (define int-pairs (stream-take-n (full-pairs s1 s2) 9 ))
39
39
(check-equal? int-pairs '((1 1 ) (1 2 ) (2 2 ) (2 1 ) (2 3 ) (1 3 ) (3 3 ) (3 1 ) (3 2 )))
40
40
)
41
41
)
Original file line number Diff line number Diff line change 29
29
(define s1 (list-to-stream '(1 2 3 )))
30
30
(define s2 (list-to-stream '(1 2 3 )))
31
31
(define s3 (list-to-stream '(1 2 3 )))
32
- (check-equal? (stream-to-list (triples s1 s2 s3) 20 ) '((1 1 1 )
32
+ (check-equal? (stream-take-n (triples s1 s2 s3) 20 ) '((1 1 1 )
33
33
(1 1 2 )
34
34
(2 2 2 )
35
35
(1 2 2 )
43
43
(define s1 (stream-enumerate-interval 1 10 ))
44
44
(define s2 (stream-enumerate-interval 1 10 ))
45
45
(define s3 (stream-enumerate-interval 1 10 ))
46
- (check-equal? (stream-to-list (pythagorean (triples s1 s2 s3)) 2 ) '((3 4 5 ) (6 8 10 )))
46
+ (check-equal? (stream-take-n (pythagorean (triples s1 s2 s3)) 2 ) '((3 4 5 ) (6 8 10 )))
47
47
)
48
48
))
Original file line number Diff line number Diff line change 43
43
(define s2 (list-to-stream '(2 -4 5 8 9 )))
44
44
(define weight (lambda (x) (abs x)))
45
45
(define s3 (merge-weighted s1 s2 weight))
46
- (check-equal? (stream-to-list s3 10 ) '(1 2 2 3 -4 5 5 7 8 9 ))
46
+ (check-equal? (stream-take-n s3 10 ) '(1 2 2 3 -4 5 5 7 8 9 ))
47
47
)
48
48
49
49
(test-case "Test for pairs-weighted with weight = i+j "
50
50
(define s1 (list-to-stream '(1 2 3 )))
51
51
(define s2 (list-to-stream '(1 2 3 )))
52
52
(define weight (lambda (pair) (+ (car pair) (cadr pair))))
53
53
(define s3 (pairs-weighted s1 s2 weight))
54
- (check-equal? (stream-to-list s3 10 ) '((1 1 ) (1 2 ) (1 3 ) (2 2 ) (2 3 ) (3 3 )))
54
+ (check-equal? (stream-take-n s3 10 ) '((1 1 ) (1 2 ) (1 3 ) (2 2 ) (2 3 ) (3 3 )))
55
55
)
56
56
57
57
(test-case "Test for pairs-weighted with weight = 2j + 3j + 5ij "
61
61
(* 3 j)
62
62
(* 5 i j)))))
63
63
(define result (pairs-weighted not-div-by-2-3-5 not-div-by-2-3-5 weight))
64
- (check-equal? (stream-to-list result 5 ) '((1 1 ) (1 7 ) (1 11 ) (1 13 ) (1 17 ))))
64
+ (check-equal? (stream-take-n result 5 ) '((1 1 ) (1 7 ) (1 11 ) (1 13 ) (1 17 ))))
65
65
)
Original file line number Diff line number Diff line change 67
67
(test-case "Test for interleave "
68
68
(define s1 (list-to-stream '(1 2 3 4 5 )))
69
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 ))
70
+ (check-equal? (stream-take-n (interleave s1 s2) 10 ) '(1 6 2 7 3 8 4 9 5 10 ))
71
71
)
72
72
73
73
(test-case "Test for pairs "
74
74
(define s1 (list-to-stream '(1 2 3 )))
75
75
(define s2 (list-to-stream '(1 2 3 )))
76
- (define int-pairs (stream-to-list (pairs s1 s2) 4 ))
76
+ (define int-pairs (stream-take-n (pairs s1 s2) 4 ))
77
77
(check-equal? int-pairs '((1 1 ) (1 2 ) (2 2 ) (1 3 )))
78
78
)
79
79
)
Original file line number Diff line number Diff line change 73
73
(stream-enumerate-interval (+ low 1 ) high))))
74
74
75
75
;; Testing utility functions
76
- (define (stream-to-list stream n)
76
+ (define (stream-take-n stream n)
77
77
(if (or (= n 0 )
78
78
(stream-null? stream ))
79
79
'()
80
80
(cons (stream-car stream )
81
- (stream-to-list (stream-cdr stream )
81
+ (stream-take-n (stream-cdr stream )
82
82
(- n 1 )))))
83
83
(define (list-to-stream lst)
84
84
(if (null? lst)
152
152
(test-case "Test for stream-map "
153
153
(define s3 (list-to-stream '(1 2 3 4 5 )))
154
154
(define squared (stream-map (lambda (x) (* x x)) s3))
155
- (check-equal? (stream-to-list squared 5 ) '(1 4 9 16 25 ))
155
+ (check-equal? (stream-take-n squared 5 ) '(1 4 9 16 25 ))
156
156
)
157
157
158
158
(test-case "Test for stream-filter "
159
159
(define s3 (list-to-stream '(1 2 3 4 5 )))
160
160
(define even (stream-filter even? s3))
161
- (check-equal? (stream-to-list even 5 ) '(2 4 ))
161
+ (check-equal? (stream-take-n even 5 ) '(2 4 ))
162
162
)
163
163
164
164
(test-case "Test for stream-for-each "
175
175
176
176
(test-case "Test for stream-enumerate-interval "
177
177
(define s (stream-enumerate-interval 5 10 ))
178
- (check-equal? (stream-to-list s 10 ) '(5 6 7 8 9 10 ))
178
+ (check-equal? (stream-take-n s 10 ) '(5 6 7 8 9 10 ))
179
179
)
180
180
)
You can’t perform that action at this time.
0 commit comments