From: Ramakrishnan Muthukrishnan Date: Wed, 11 Aug 2010 14:57:23 +0000 (+0530) Subject: solution to 2.40. Extension of the text discussions. X-Git-Url: https://git.rkrishnan.org/specifications/vdrive/flags//%22%3C?a=commitdiff_plain;h=f161aa3268f9e5e96e718628612e3c13b389c617;p=sicp.git solution to 2.40. Extension of the text discussions. --- 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