3 [clojure.contrib test-is]))
5 (defn accumulate [combiner null-value term a next b]
9 (accumulate combiner null-value term (next a) next b))))
11 (def sum (fn [a b] (accumulate + 0 identity a inc b)))
13 (def sum-cube (fn [a b] (accumulate + 0 cube a inc b)))
15 (def prod (fn [a b] (accumulate * 1 identity a inc b)))
17 (defn iaccumulate [combiner null-value term a next b]
18 (iter combiner null-value term a next b null-value))
20 (defn iter [combiner null-value term a next b result]
23 (iter combiner null-value term (next a) next b (combiner (term a) result))))
25 (def isum (fn [a b] (iaccumulate + 0 identity a inc b)))
27 (def isum-cube (fn [a b] (iaccumulate + 0 cube a inc b)))
29 (def iprod (fn [a b] (iaccumulate * 1 identity a inc b)))
31 (deftest test-sum-of-integers-from-1-to-10
32 (is (= (sum 1 10) (reduce + (range 1 11)))))
34 (deftest test-sum-cube-of-integers-from-1-to-10
35 (is (= (sum-cube 1 10) (reduce + (map cube (range 1 11))))))
37 (deftest test-prod-of-ints-from-1-to-10
38 (is (= (prod 1 10) (reduce * (range 1 11)))))
40 (deftest test-isum-of-integers-from-1-to-10
41 (is (= (isum 1 10) (reduce + (range 1 11)))))
43 (deftest test-isum-cube-of-integers-from-1-to-10
44 (is (= (isum-cube 1 10) (reduce + (map cube (range 1 11))))))
46 (deftest test-iprod-of-ints-from-1-to-10
47 (is (= (iprod 1 10) (reduce * (range 1 11)))))