"hi there""> "hi there"">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dispatch on Type (one-less x) => x-1 if x is a <number>

Similar presentations


Presentation on theme: "Dispatch on Type (one-less x) => x-1 if x is a <number>"— Presentation transcript:

1 Dispatch on Type (one-less x) => x-1 if x is a <number>
=> (tail x) if x is a <pair> => x otherwise (define (one-less <function>) (method ((x <object>)) (cond ((instance? x <number>) (- x 1)) ((instance? x <pair>) (tail x)) (else: x))))

2 Generic Functions (define-generic-function one-less (x))
(add-method one-less (method ((x <object>)) x)) (add-method one-less (method ((x <number>)) (- x 1))) (add-method one-less (method ((x <pair>)) (tail x))) ? (one-less 4) => 3 ? (one-less '(1 2 3)) => (2 3) ? (one-less "hi there") => "hi there"

3 List Equality (add-method =
(method ((l1 <list>) (l2 <list>)) (cond ((null? l1) (null? l2)) ((null? l2) #f) (else: (and (= (head l1) (head l2)) (= (tail l1) (tail l2)))))))

4 Symbolic Differentiation
dx c = 0, c a constant x = 1 y = 0, y an independent variable (u + v) = u + v (uv) = v + u (u ) = nu n n-1

5 Constants Type: <const> Predicate: constant? Accessor: value Variables Type: <var> Predicates: variable? (same-variable? v1 v2) Accessor: name Sum (+ a b) Type: <sum> Predicate: sum? Accessors: addend, augend Constructor: make-sum

6 Product (* a b) Type: <prod> Predicate: product? Accessors: multiplier, multiplicand Constructor: make-prod Exponent (^ a b) Type: <expt> Predicate: exponent? Accessors: base, power Constructor: make-expt

7 (define (deriv <function>)
(method ((e <object>) (v <var>)) (cond ((constant? e) 0) ((variable? e) (if (same-variable? e v) 1 0)) ((product? e) (make-sum (make-prod (multiplier e) (deriv (multiplicand e) v)) (make-prod (deriv (multiplier e) v) (multiplicand e)))) ((sum? e) (make-sum (deriv (addend e) v) (deriv (augend e) v))) ((exponent? e) (make-prod (make-prod (power e) (make-expt (base e) (- (power e) 1))) (deriv (base e) v))))))

8 (define-class <sum> (<object>)
(addend <object>) (augend <object>)) (define (make-sum <function>) (method ((x <object>) (y <object>)) (make <sum> addend: x augend: y))) (define (sum? <function>) (method ((e <object>)) (instance? e <sum>)))

9 (define-class <name>
(<super-1> ... <super-m>) slot slot-n) slot-i can be either (key-i <type-i>) or (key-i <type-i> initialvalue-i) (make <name> key-1: val key-n: val-n)

10 (define-generic-function deriv (e v))
(add-method deriv (method ((e <object>) (v <var>)) 0)) (method ((e <var>) (v <var>)) (if (= e v) 1 0))) (method ((e <sum>) (v <var>)) (make-sum (deriv (addend e) v) (deriv (augend e) v))))

11 (add-method deriv (method ((e <prod>) (v <var>)) (make-sum (make-prod (multiplier e) (deriv (multiplicand e) v)) (make-prod (deriv (multiplier e) v) (multiplicand e))))) (method ((e <expt>) (v <var>)) (make-prod (make-prod (power e) (make-expt (base e) (- (power e) 1))) (deriv (base e) v))))

12 (add-method = (method ((x <var>) (y <var>))
(= (name x) (name y))))

13 (define-generic-function print (e))
(add-method print (method ((e <object>)) e)) (add-method print (method ((e <var>)) (name e))) (add-method print (method ((e <sum>)) (list '+ (print (addend e)) (print (augend e))))) (method ((e <prod>)) (list '* (print (multiplier e)) (print (multiplicand e))))) (method ((e <expt>)) (list '^ (print (base e)) (print (power e)))))

14 (define p (make-sum (make-expt x 2) (make-sum (make-prod 2 (make-prod x y)) (make-expt y 2)))) ? (print p) ==> (+ (^ x 2) (+ (* 2 (* x y)) (^ y 2))) ? (print (deriv p x)) ==> (+ (* (* 2 (^ x 1)) 1) (+ (+ (* 2 (+ (* x 0) (* 1 y))) (* 0 (* x y))) (* (* 2 (^ y 1)) 0)))

15 Simplify (make-sum a b): - If a or b is zero just return the other one
- If a and b are both numbers then add them as numbers. (make-product a b) - If a=0 or b=0 then 0 - If a=1 or b=1 then return the other. - If both are numbers, multiply them by hand. (make-expt a b) - If b=0 then 1 - If b=1 then a

16 (define make-sum (method ((x <object>) (y <object>)) (cond ((and (instance? x <number>) (instance? y <number>)) (+ x y)) ((= x 0) y) ((= y 0) x) (else: (make <sum> addend: x augend: y))))) (print p) ==> (+ (^ x 2) (+ (* 2 (* x y)) (^ y 2))) ? (print (deriv p x)) ==> (+ (* 2 x) (* 2 y))

17 For infix, change to (add-method print (method ((e <sum>)) (list
'+ (print (addend e)) (print (augend e))))) to ? (print p) ==> ((x ^ 2) + ((2 * (x * y)) + (y ^ 2))) ? (print (deriv p x)) ==> ((2 * x) + (2 * y))


Download ppt "Dispatch on Type (one-less x) => x-1 if x is a <number>"

Similar presentations


Ads by Google