]> git.rkrishnan.org Git - sicp.git/commitdiff
solution to 1.33
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Wed, 5 May 2010 13:11:04 +0000 (18:41 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Wed, 5 May 2010 13:11:04 +0000 (18:41 +0530)
src/sicp/ex1_33.clj [new file with mode: 0644]

diff --git a/src/sicp/ex1_33.clj b/src/sicp/ex1_33.clj
new file mode 100644 (file)
index 0000000..f9ab503
--- /dev/null
@@ -0,0 +1,25 @@
+(ns sicp.ex1_33
+  (:use [sicp utils]
+       [clojure.contrib test-is]))
+
+(defn filtered-accumulate [predicate? combiner null-value term a next b]
+  (if (> a b)
+    null-value
+    (combiner (term (if (predicate? a)
+                     a
+                     null-value))
+             (filtered-accumulate predicate?
+                                  combiner
+                                  null-value
+                                  term
+                                  (next a)
+                                  next
+                                  b))))
+
+(deftest test-filtered-sum-of-primes-from-1-to-10
+  (is (= (filtered-accumulate prime? + 0 identity 1 inc 10)
+        (reduce + (filter prime? (range 1 11))))))
+
+(deftest test-filtered-prod-of-relative-primes-of-10
+  (is (= (filtered-accumulate #(= 1 (gcd % 10)) * 1 identity 1 inc 10)
+        (reduce * (filter #(= 1 (gcd % 10)) (range 1 11))))))
\ No newline at end of file