]> git.rkrishnan.org Git - sicp.git/commitdiff
solution to 2.56
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Sat, 4 Sep 2010 03:37:05 +0000 (09:07 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Sat, 4 Sep 2010 03:37:05 +0000 (09:07 +0530)
src/sicp/ex2_56.clj [new file with mode: 0644]

diff --git a/src/sicp/ex2_56.clj b/src/sicp/ex2_56.clj
new file mode 100644 (file)
index 0000000..960759b
--- /dev/null
@@ -0,0 +1,38 @@
+(ns sicp.ex2_56
+  (:use [sicp.ch2_3 :exclude (deriv)]
+        [sicp.utils]
+        [clojure.test]))
+
+(declare exponentiation? exponent base make-exponentiation)
+
+(defn deriv [exp var]
+  (cond (number? exp) 0
+        (variable? exp) (if (same-variable? exp var) 1 0)
+        (sum? exp) (make-sum (deriv (addend exp) var)
+                             (deriv (augend exp) var))
+        (product? exp) (make-sum (make-product (multiplier exp)
+                                               (deriv (multiplicand exp) var))
+                                 (make-product (deriv (multiplier exp) var)
+                                               (multiplicand exp)))
+        (exponentiation? exp) (make-product (exponent exp)
+                                            (make-product (make-exponentiation (base exp)
+                                                                               (- (exponent exp) 1))
+                                                          (deriv (base exp) var)))
+        :else (error "unknown expression type -- derive")))
+
+(defn exponentiation? [exp]
+  (= (first exp) '**))
+
+(defn base [exp]
+  (second exp))
+
+(defn exponent [exp]
+  (second (rest exp)))
+
+(defn make-exponentiation [b n]
+  (cond (=number? b 1) 1
+        (=number? b 0) 0        
+        (=number? n 1) b
+        (=number? n 0) 1
+        (and (number? b) (number? n)) (Math/pow b n)
+        :else (list '** b n)))
\ No newline at end of file