From 39073af470776acb0e36a000a27673306dc20603 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Fri, 9 Apr 2010 15:34:48 +0530 Subject: [PATCH] Added solution to 1.16. --- chapter1/ch1_2.clj | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/chapter1/ch1_2.clj b/chapter1/ch1_2.clj index 89ccec5..d6bb552 100644 --- a/chapter1/ch1_2.clj +++ b/chapter1/ch1_2.clj @@ -478,3 +478,16 @@ TRACE t2494: => -0.39980345741334 (defn even? [x] (= (rem x 2) 0)) + +(defn square [x] + (* x x)) + +;; exercise 1.16: +(defn expt [b n] + (expt-iter b n 1)) + +(defn expt-iter [b n a] + (cond (= n 0) a + (even? n) (expt-iter (square b) (/ n 2) a) + :else (expt-iter b (- n 1) (* a b)))) + -- 2.45.2