-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_2_41.clj
40 lines (34 loc) · 1.06 KB
/
ex_2_41.clj
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
(ns sicp.chapter-2.part-2.ex-2-41
(:require
[sicp.chapter-2.part-2.book-2-2 :as b22]))
; 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.
(defn duplets
[n]
(b22/flatmap (fn [i]
(map (fn [j] (list i j))
(b22/enumerate-interval 1 n)))
(b22/enumerate-interval 1 n)))
(defn triplets
[n]
(b22/flatmap (fn [i]
(map (fn [j] (cons i j))
(duplets n)))
(b22/enumerate-interval 1 n)))
(defn check-triplet?
[triplet]
(and (< (b22/list-ref triplet 0)
(b22/list-ref triplet 1))
(< (b22/list-ref triplet 1)
(b22/list-ref triplet 2))))
(defn check-triplet-sum?
[triplet sum]
(= sum (+ (b22/list-ref triplet 0)
(b22/list-ref triplet 1)
(b22/list-ref triplet 2))))
(defn find-triplets
[n s]
(filter (fn [i] (check-triplet-sum? i s))
(filter check-triplet? (triplets n))))