3.72 Exercise 3.72
(define (find-three-consecutive-by f g s) (if (stream-null? s) the-empty-stream (let ((first (stream-car s)) (rest (stream-cdr s))) (if (stream-null? rest) the-empty-stream (let ((second (stream-car rest)) (rrest (stream-cdr rest))) (if (stream-null? rrest) the-empty-stream (let ((third (stream-car rrest))) (if (= (f first) (f second) (f third)) (cons-stream (cons (f first) (map g (list first second third))) (find-three-consecutive-by f g (drop-while (lambda (x) (= (f x) (f first))) (stream-cdr rrest)))) (find-three-consecutive-by f g rest)))))))))
(define (weight-squared p) (+ (square (car p)) (square (cadr p))))
(define squares-three-ways (find-three-consecutive-by car cadr (stream-map (lambda (p) (list (weight-squared p) p)) (weighted-pairs weight-squared integers integers))))
> (display-stream (take-stream squares-three-ways 10))
(325 (10 15) (6 17) (1 18))
(425 (13 16) (8 19) (5 20))
(650 (17 19) (11 23) (5 25))
(725 (14 23) (10 25) (7 26))
(845 (19 22) (13 26) (2 29))
(850 (15 25) (11 27) (3 29))
(925 (21 22) (14 27) (5 30))
(1025 (20 25) (8 31) (1 32))
(1105 (23 24) (12 31) (9 32))
(1250 (25 25) (17 31) (5 35))
'done