Download presentation
Presentation is loading. Please wait.
Published byNathaniel Hunt Modified over 9 years ago
1
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
2
CONSTANTS: Type: Predicate: const? represented concretely as s VARIABLES: Type: Predicate: var? (same-var? v1 v2) represented concretely as s
3
SUM: Type: 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: 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
4
EXPONENT: (^ a b) Type: Predicate: expt? Accessors: expt-base, expt-power Constructor: make-expt Contract: If x = (make-expt a b) then (^ (expt-base x) (expt-power x)) = a b
5
(define (deriv e (v )) (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)))))))
6
(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 "...")))
7
(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.
8
(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 :addend x :augend y))))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.