From: Ramakrishnan Muthukrishnan Date: Wed, 5 May 2010 13:11:04 +0000 (+0530) Subject: solution to 1.33 X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/frontends/simplejson/?a=commitdiff_plain;h=0d86111748933e0eb7372fe4010a091ed23e2eb6;p=sicp.git solution to 1.33 --- diff --git a/src/sicp/ex1_33.clj b/src/sicp/ex1_33.clj new file mode 100644 index 0000000..f9ab503 --- /dev/null +++ b/src/sicp/ex1_33.clj @@ -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