File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 2525 习题完成情况:
2626 - 章节一: 43/46
2727 - 章节二: 88/97
28- - 章节三: 62 /82
28+ - 章节三: 63 /82
2929 - 章节四: TODO
3030 - 章节五: TODO
3131* 运行
Original file line number Diff line number Diff line change 1+ #lang racket
2+ (require "stream.rkt " )
3+ (require "infinite-stream.rkt " )
4+
5+ (define (triples s t u)
6+ (if (or (stream-null? s)
7+ (stream-null? t)
8+ (stream-null? u))
9+ the-empty-stream
10+ (cons-stream
11+ (list (stream-car s) (stream-car t) (stream-car u))
12+ (interleave
13+ (stream-map (lambda (pair) (list (stream-car s) (car pair) (cadr pair)))
14+ (stream-cdr (pairs t u)))
15+ (triples (stream-cdr s) (stream-cdr t) (stream-cdr u))))))
16+ (define (square x)
17+ (* x x))
18+
19+ (define (pythagorean)
20+ (stream-filter (lambda (triple)
21+ (= (+ (square (car triple))
22+ (square (cadr triple)))
23+ (square (caddr triple))))
24+ (triples integers integers integers)))
25+ (module+ test
26+ (require rackunit)
27+
28+ (test-case "Test for triples "
29+ (define s1 (list-to-stream '(1 2 3 )))
30+ (define s2 (list-to-stream '(1 2 3 )))
31+ (define s3 (list-to-stream '(1 2 3 )))
32+ (check-equal? (stream-to-list (triples s1 s2 s3) 10 ) '((1 1 1 ) (1 1 2 ) (2 2 2 ) (1 2 2 ) (2 2 3 )))
33+ )
34+ (test-case "Test for pythagorean "
35+ (check-equal? (stream-to-list (pythagorean) 2 ) '((3 4 5 ) (6 8 10 )))
36+ )
37+ )
You can’t perform that action at this time.
0 commit comments