From: Ramakrishnan Muthukrishnan Date: Fri, 9 Apr 2010 10:04:48 +0000 (+0530) Subject: Added solution to 1.16. X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/file/URI:LIT:krugkidfnzsc4/@@named=/pb.xhtml?a=commitdiff_plain;h=39073af470776acb0e36a000a27673306dc20603;p=sicp.git Added solution to 1.16. --- 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)))) +