-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.41.rkt
51 lines (44 loc) · 1.29 KB
/
2.41.rkt
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
49
50
51
#lang sicp
;; Exercise 2.41:
;; Write a procedure to find all ordered triples of distinct positive integers i , j , and k less than or equal to a given integer n that sum to a given integer s .
(define (append list1 list2)
(if (null? list1)
list2
(cons (car list1)
(append (cdr list1)
list2))))
(define (flatmap proc seq)
(accumulate append nil (map proc seq)))
(define (add-to-s? pair s)
(= s (+ (car pair) (cadr pair))))
(define (make-pair-sum pair)
(list (car pair)
(cadr pair)
(+ (car pair) (cadr pair))))
(define (prime-sum-pairs n s)
(map make-pair-sum
(filter
()
(flatmap
(lambda (i)
(map (lambda (j)
(list i j))
(enumerate-interval
1
(- i 1))))
(enumerate-interval 1 n)))))
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low
(enumerate-interval
(+ low 1)
high))))
(define (filter predicate sequence)
(cond ((null? sequence) nil)
((predicate (car sequence))
(cons (car sequence)
(filter predicate
(cdr sequence))))
(else (filter predicate
(cdr sequence)))))