From 59bdd64b183c491b181d0b3f2a63f8758ed4fd0b Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Fri, 9 Apr 2010 19:07:38 +0530 Subject: [PATCH] Adding solution to 1.17. --- chapter1/ch1_2.clj | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/chapter1/ch1_2.clj b/chapter1/ch1_2.clj index d6bb552..ed57832 100644 --- a/chapter1/ch1_2.clj +++ b/chapter1/ch1_2.clj @@ -491,3 +491,23 @@ TRACE t2494: => -0.39980345741334 (even? n) (expt-iter (square b) (/ n 2) a) :else (expt-iter b (- n 1) (* a b)))) +;; exercise 1.17: +(defn mult [a b] + (if (= b 0) + 0 + (+ a (mult a (- b 1))))) + +;; product = 2 * (a * (b/2)) for even b +;; = a + (a * (b - 1)) for odd b +(defn fast-mult [a b] + (cond (= b 0) 0 + (= b 1) a + (even? b) (twice (fast-mult a (half b))) + :else (+ a (fast-mult a (- b 1))))) + +;; double +(defn twice [x] + (* 2 x)) + +(defn half [x] + (/ x 2)) \ No newline at end of file -- 2.45.2