From: Ramakrishnan Muthukrishnan Date: Fri, 9 Apr 2010 13:37:38 +0000 (+0530) Subject: Adding solution to 1.17. X-Git-Url: https://git.rkrishnan.org/simplejson/(%5B%5E?a=commitdiff_plain;h=59bdd64b183c491b181d0b3f2a63f8758ed4fd0b;p=sicp.git Adding solution to 1.17. --- 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