Skip to content

Commit 01ffab0

Browse files
committed
Add implementation of exercise 3-68
1 parent 96646ae commit 01ffab0

File tree

2 files changed

+38
-1
lines changed

2 files changed

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

chapter3/exercise3-69.rkt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
)

0 commit comments

Comments
 (0)