]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex1_43.clj
Lazy version of evaluator and tests.
[sicp.git] / src / sicp / ex1_43.clj
1 (ns sicp.ex1_43
2   (:use [clojure.contrib test-is]
3         [sicp utils]
4         [sicp ch1_3]
5         [sicp ex1_42]))
6
7 ;; older iterative implementation
8 (defn- repeated-i [f1 f2 m]
9   (cond (= m 0) f1
10         (= m 1) f2
11         :else (repeated-i f1 (compose f2 f1) (- m 1))))
12
13 (defn repeated-iterative [f n]
14   (repeated-i f (compose f f) (- n 1)))
15
16 (defn repeated [f n]
17   (if (= n 1)
18     f
19     (compose f (repeated f (- n 1)))))
20
21 (deftest test-repeated-square-twotimes-of-5
22   (is (= ((repeated square 2) 5)
23          625)))
24
25 (deftest test-repeated-square-twotimes-of-2
26   (is (= ((repeated square 2) 2)
27          16)))
28
29 (deftest test-repeated-inc-of-2-by-5
30   (is (= ((repeated inc 5) 2) 7)))