From: Ramakrishnan Muthukrishnan Date: Sat, 4 Sep 2010 03:37:05 +0000 (+0530) Subject: solution to 2.56 X-Git-Url: https://git.rkrishnan.org/specifications/somewhere?a=commitdiff_plain;h=fe6179a3f59ec8a975446ba4cb8286498dfbffff;p=sicp.git solution to 2.56 --- diff --git a/src/sicp/ex2_56.clj b/src/sicp/ex2_56.clj new file mode 100644 index 0000000..960759b --- /dev/null +++ b/src/sicp/ex2_56.clj @@ -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