-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-3.72.scm
48 lines (42 loc) · 1.57 KB
/
ex-3.72.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(require "./stream-lib")
(require "./ex-3.70")
(define (square-weight pair)
(let ((i (car pair))
(j (cadr pair)))
(+ (* i i) (* j j))))
(define square-weighted-pairs
(weighted-pairs integers
integers
square-weight))
(define (stream-group-by s key-selector)
(define (go s key elements)
(if (stream-null? s)
(cons-stream (cons key elements) the-empty-stream)
(let ((k (key-selector (stream-car s))))
(if (equal? k key)
(go (stream-cdr s) key (cons (stream-car s) elements))
(cons-stream (cons key elements)
(go (stream-cdr s) k (list (stream-car s))))))))
(if (stream-null? s)
the-empty-stream
(go (stream-cdr s) (key-selector (stream-car s)) (list (stream-car s)))))
(define (generate-square-sum)
(define (go s prev count)
(if (stream-null? s)
the-empty-stream
(let ((w (square-weight (stream-car s))))
(if (equal? w prev)
(go (stream-cdr s) prev (+ count 1))
(if (= count 3)
(cons-stream prev
(go (stream-cdr s) prev 1))
(go (stream-cdr s) w 1))))))
(go square-weighted-pairs #f 1))
(define (generate-square-sum)
(stream-filter (lambda (g) (= (length (cdr g)) 3))
(stream-group-by square-weighted-pairs square-weight)))
(define (main args)
(display-stream (stream-take (generate-square-sum)
6))
(display-stream (stream-group-by (stream-enumerate-interval 1 20)
(lambda (x) (< x 10)))))