-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.41.scm
31 lines (25 loc) · 905 Bytes
/
2.41.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
(define nil '())
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
(define (accumulate op initial items)
(if (null? items)
initial
(op (car items)
(accumulate op initial (cdr items)))))
(define (flatmap proc items)
(accumulate append nil (map proc items)))
(define (ordered-triples n)
(flatmap (lambda (third)
(flatmap (lambda (second)
(map (lambda (first) (list first second third))
(enumerate-interval 1 second)))
(enumerate-interval 1 third)))
(enumerate-interval 1 n)))
(define (find-triples n s)
(filter (lambda (triple) (= s (+ (car triple)
(cadr triple)
(caddr triple))))
(ordered-triples n)))
(find-triples 5 10) ; ((3 3 4) (2 4 4) (2 3 5) (1 4 5))