From f161aa3268f9e5e96e718628612e3c13b389c617 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Wed, 11 Aug 2010 20:27:23 +0530 Subject: [PATCH] solution to 2.40. Extension of the text discussions. --- src/sicp/ex2_40.clj | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/sicp/ex2_40.clj diff --git a/src/sicp/ex2_40.clj b/src/sicp/ex2_40.clj new file mode 100644 index 0000000..3c091cd --- /dev/null +++ b/src/sicp/ex2_40.clj @@ -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 -- 2.45.2