80 likes | 176 Views
D x (c) = 0 D x (x) = 1 D x (y) = 0 for y an independent variable D x (u+v) = D x (u) + D x (v) D x (uv) = u D x (v) + v D x (u) D x (u n ) = nu n-1 D x (u) for n > 0. CONSTANTS: Type: <const> Predicate: const? represented concretely as <number>s VARIABLES: Type: <var>
E N D
Dx(c) = 0Dx(x) = 1Dx(y) = 0 for y an independent variableDx(u+v) = Dx(u) + Dx(v)Dx(uv) = u Dx(v) + v Dx(u)Dx(un) = nun-1 Dx(u) for n > 0
CONSTANTS: • Type: <const> • Predicate: const? • represented concretely as <number>s • VARIABLES: • Type: <var> • Predicate: var? (same-var? v1 v2) • represented concretely as <symbol>s
SUM: • Type: <sum> • Predicate: sum? • Accessors: sum-addend, sum-augend • Constructor: make-sum • Contract: If x = (make-sum a b) • then (+ (sum-addend x) (sum-augend x)) = a + b • PRODUCT: • Type: <prod> • Predicate: prod? • Accessors: prod-multiplier, prod-multiplicand • Constructor: make-prod • Contract: If x = (make-prod a b) • then (* (prod-multiplier x) (prod-multiplicand x)) = ab
EXPONENT: (^ a b) • Type: <expt> • Predicate: expt? • Accessors: expt-base, expt-power • Constructor: make-expt • Contract: If x = (make-expt a b) • then (^ (expt-base x) (expt-power x)) = ab
(define (deriv e (v <var>)) (cond ((const? e) 0) ((var? e) (if (same-var? e v) 1 0)) ((sum? e) (make-sum (deriv (get-sum-addend e) v) (deriv (get-sum-augend e) v))) ((prod? e) (make-sum (make-prod (get-prod-multiplier e) (deriv (get-prod-multiplicand e) v)) (make-prod (deriv (get-prod-multiplier e) v) (get-prod-multiplicand e)))) ((expt? e) (make-prod (make-prod (get-expt-power e) (make-expt (get-expt-base e) (make-sum (get-expt-power e) -1))) (deriv (get-expt-base e) v)))))))
(define (make-sum x y) (list '+ x y)) (define (sum? x) (and (list? x) (= 3 (length x)) (eq? '+ (first x)))) (define (get-sum-addend x) (if (sum? x) (second x) (error "..."))) (define (get-sum-augend x) (if (sum? x) (third x) (error "...")))
(make-sum a b) ==> • If a or b is zero, just return the other one • If a and b are both numbers, add them. • (make-product a b) ==> • If a=0 or b=0, return 0 • If a=1 or b=1, return the other. • If both are numbers, multiply them. • (make-expt a b) ==> • If a=0 or a=1, return a • If b=0, return 1 • If b=1, return a • If both are numbers, do the computation.
(define (make-sum x y) (cond ((and (const? x) (const? y)) (+ x y)) ((and (const? x) (= x 0)) y) ((and (const? y) (= y 0)) x) (else (make <sum> :addend x :augend y))))