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

diff --git a/src/sicp/ex2_40.clj b/src/sicp/ex2_40.clj
new file mode 100644 (file)
index 0000000..3c091cd
--- /dev/null
@@ -0,0 +1,26 @@
+(ns sicp.ex2_40
+  (:use [sicp.ch2-2 :only (enumerate-interval accumulate append)]
+        [sicp.ch1-2 :only (prime?)]))
+
+;; Define a procedure unique-pairs that, given an integer n,
+;; generates the sequence of pairs (i,j) with 1< j< i< n.
+;; Use unique-pairs to simplify the definition of prime-sum-pairs
+;; given above.
+(defn unique-pairs [n]
+  (accumulate append
+              nil
+              (map (fn [i]
+                     (map (fn [j] (list i j))
+                          (enumerate-interval 1 (- i 1))))
+                   (enumerate-interval 1 n))))
+
+(defn prime-sum? [pair]
+  (prime? (+ (first pair)
+             (first (rest pair)))))
+
+(defn make-pair-sum [pair]
+  (list (first pair) (first (rest pair)) (+ (first pair)
+                                            (first (rest pair)))))
+
+(defn prime-sum-pairs [n]
+  (map make-pair-sum (filter prime-sum? (unique-pairs n))))
\ No newline at end of file