]> git.rkrishnan.org Git - sicp.git/commitdiff
solution to 2.41
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Wed, 11 Aug 2010 14:57:43 +0000 (20:27 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Wed, 11 Aug 2010 14:57:43 +0000 (20:27 +0530)
src/sicp/ex2_41.clj [new file with mode: 0644]

diff --git a/src/sicp/ex2_41.clj b/src/sicp/ex2_41.clj
new file mode 100644 (file)
index 0000000..e43773a
--- /dev/null
@@ -0,0 +1,21 @@
+(ns sicp.ex2_41
+  (:use [sicp.ch2-2 :only (enumerate-interval accumulate append)]))
+
+(defn triplets [n]
+  (accumulate append
+              nil
+              (accumulate append
+                          nil
+                          (map (fn [i]
+                                 (map (fn [j]
+                                        (map (fn [k] (list i j k))
+                                             (enumerate-interval 1 (- j 1))))
+                                      (enumerate-interval 1 (- i 1))))
+                               (enumerate-interval 1 n)))))
+
+(defn sum-triplets [triplet]
+  (accumulate + 0 triplet))
+
+(defn ordered-triplets [n sum]
+  (filter #(= (sum-triplets %) sum)
+          (triplets n)))
\ No newline at end of file