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 1818 (define s1 (list-to-stream '(1 2 3 4 )))
1919 (define s2 (list-to-stream '(10 20 30 40 )))
2020 (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 ))
2222 )
2323
2424 (test-case "Test for different length streams(stop at shortest) "
2525 (define s1 (list-to-stream '(1 2 )))
2626 (define s2 (list-to-stream '(10 20 30 40 )))
2727 (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 ))
2929 )
3030 )
Original file line number Diff line number Diff line change 1616 (define s2 (list-to-stream '(3 4 5 6 )))
1717 (define s3 (mul-streams s1 s2))
1818 (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 ))
2020 )
2121 (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 ))
2323 ;; n = 0, n+1=1 的阶乘是1
2424 ;; n = 1, n+1=2 的阶乘是2
2525 ;; n = 2, n+1=3 的阶乘是6
Original file line number Diff line number Diff line change 1111
1212 (test-case "Test for partial-sums "
1313 (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 ))
1515 )
1616)
Original file line number Diff line number Diff line change 2727 (define s1 (list-to-stream '(1 2 3 5 7 )))
2828 (define s2 (list-to-stream '(2 4 5 8 9 )))
2929 (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 ))
3131 )
3232 (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 ))
3434 )
3535 )
Original file line number Diff line number Diff line change 1313 (test-case "Test for expand "
1414 (define x (expand 3 8 10 ))
1515 ;; 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 ))
1717
1818 (define s (expand 1 7 10 ))
1919 ;; (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 3535 (test-case "Test for full-pairs "
3636 (define s1 (list-to-stream '(1 2 3 )))
3737 (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 ))
3939 (check-equal? int-pairs '((1 1 ) (1 2 ) (2 2 ) (2 1 ) (2 3 ) (1 3 ) (3 3 ) (3 1 ) (3 2 )))
4040 )
4141 )
Original file line number Diff line number Diff line change 2929 (define s1 (list-to-stream '(1 2 3 )))
3030 (define s2 (list-to-stream '(1 2 3 )))
3131 (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 )
3333 (1 1 2 )
3434 (2 2 2 )
3535 (1 2 2 )
4343 (define s1 (stream-enumerate-interval 1 10 ))
4444 (define s2 (stream-enumerate-interval 1 10 ))
4545 (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 )))
4747 )
4848 ))
Original file line number Diff line number Diff line change 4343 (define s2 (list-to-stream '(2 -4 5 8 9 )))
4444 (define weight (lambda (x) (abs x)))
4545 (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 ))
4747 )
4848
4949 (test-case "Test for pairs-weighted with weight = i+j "
5050 (define s1 (list-to-stream '(1 2 3 )))
5151 (define s2 (list-to-stream '(1 2 3 )))
5252 (define weight (lambda (pair) (+ (car pair) (cadr pair))))
5353 (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 )))
5555 )
5656
5757 (test-case "Test for pairs-weighted with weight = 2j + 3j + 5ij "
6161 (* 3 j)
6262 (* 5 i j)))))
6363 (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 ))))
6565 )
Original file line number Diff line number Diff line change 6767 (test-case "Test for interleave "
6868 (define s1 (list-to-stream '(1 2 3 4 5 )))
6969 (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 ))
7171 )
7272
7373 (test-case "Test for pairs "
7474 (define s1 (list-to-stream '(1 2 3 )))
7575 (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 ))
7777 (check-equal? int-pairs '((1 1 ) (1 2 ) (2 2 ) (1 3 )))
7878 )
7979 )
Original file line number Diff line number Diff line change 7373 (stream-enumerate-interval (+ low 1 ) high))))
7474
7575;; Testing utility functions
76- (define (stream-to-list stream n)
76+ (define (stream-take-n stream n)
7777 (if (or (= n 0 )
7878 (stream-null? stream ))
7979 '()
8080 (cons (stream-car stream )
81- (stream-to-list (stream-cdr stream )
81+ (stream-take-n (stream-cdr stream )
8282 (- n 1 )))))
8383(define (list-to-stream lst)
8484 (if (null? lst)
152152 (test-case "Test for stream-map "
153153 (define s3 (list-to-stream '(1 2 3 4 5 )))
154154 (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 ))
156156 )
157157
158158 (test-case "Test for stream-filter "
159159 (define s3 (list-to-stream '(1 2 3 4 5 )))
160160 (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 ))
162162 )
163163
164164 (test-case "Test for stream-for-each "
175175
176176 (test-case "Test for stream-enumerate-interval "
177177 (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 ))
179179 )
180180 )
You can’t perform that action at this time.
0 commit comments