5 ;;; differentiation of infix expressions
6 ;; part a. Assume proper brackets around compound expressions.
12 (defn exponentiation? [exp]
24 (defn same-variable? [v1 v2]
29 (defn =number? [exp num]
30 (and (number? exp) (= exp num)))
32 (defn make-sum [a1 a2]
33 (cond (=number? a1 0) a2
35 (and (number? a1) (number? a2)) (+ a1 a2)
36 :else (list a1 '+ a2)))
38 (defn make-product [m1 m2]
39 (cond (or (=number? m1 0) (=number? m2 0)) 0
42 (and (number? m1) (number? m2)) (* m1 m2)
43 :else (list m1 '* m2)))
46 (and (list? x) (= (second x) '+)))
55 (and (list? x) (= (second x) '*)))
60 (defn multiplicand [p]
63 (defn make-exponentiation [b n]
64 (cond (=number? b 1) 1
68 (and (number? b) (number? n)) (Math/pow b n)
69 :else (list b '** n)))
73 (variable? exp) (if (same-variable? exp var) 1 0)
74 (sum? exp) (make-sum (deriv (addend exp) var)
75 (deriv (augend exp) var))
76 (product? exp) (make-sum (make-product (multiplier exp)
77 (deriv (multiplicand exp) var))
78 (make-product (deriv (multiplier exp) var)
80 (exponentiation? exp) (make-product (exponent exp)
81 (make-product (make-exponentiation (base exp)
83 (deriv (base exp) var)))
84 :else (str "unknown expression type -- derive " exp)))