5 (defn exponentiation? [exp]
17 (defn same-variable? [v1 v2]
22 (defn =number? [exp num]
23 (and (number? exp) (= exp num)))
25 (defn make-sum [a1 a2]
26 (cond (=number? a1 0) a2
28 (and (number? a1) (number? a2)) (+ a1 a2)
29 :else (list '+ a1 a2)))
31 (defn make-product [m1 m2]
32 (cond (or (=number? m1 0) (=number? m2 0)) 0
35 (and (number? m1) (number? m2)) (* m1 m2)
36 :else (list '* m1 m2)))
39 (and (list? x) (= (first x) '+)))
45 (reduce make-sum 0 (rest (rest s))))
48 (and (list? x) (= (first x) '*)))
53 (defn multiplicand [p]
54 (reduce make-product 1 (rest (rest p))))
56 (defn make-exponentiation [b n]
57 (cond (=number? b 1) 1
61 (and (number? b) (number? n)) (Math/pow b n)
62 :else (list '** b n)))
66 (variable? exp) (if (same-variable? exp var) 1 0)
67 (sum? exp) (make-sum (deriv (addend exp) var)
68 (deriv (augend exp) var))
69 (product? exp) (make-sum (make-product (multiplier exp)
70 (deriv (multiplicand exp) var))
71 (make-product (deriv (multiplier exp) var)
73 (exponentiation? exp) (make-product (exponent exp)
74 (make-product (make-exponentiation (base exp)
76 (deriv (base exp) var)))
77 :else (str "unknown expression type -- derive " exp)))