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

Slides:



Advertisements
Similar presentations
Joshua Eckroth The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the.
Advertisements

6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation.
 Product : the answer to an multiplication problem.  Factor : any of the numbers multiplied together in an problem.  Grouping : the order in which.
Fall 2008Programming Development Techniques 1 Topic 10 Example: Symbolic Differentiation Section October 2008.
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
Rules for Differentiating Univariate Functions Given a univariate function that is both continuous and smooth throughout, it is possible to determine its.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Multiplication properties. Multiplication properties.
Systems of Linear Equations
 To add numbers in scientific notation: 1) Add the constants 2) Keep the exponent the same  Example: (2.1 x 10 5 ) + (3.2 x 10 5 ) = ( ) x 10.
Taks Objective 2 Properties and attributes of function.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
Order of Operations Chapter 1 Section 2 Part 1 and Part 2.
Warm-ups 1.) 2 2.) 3 3.) 4 4.) (3460) = = 4222 = 822 = 162 = 32 Solve: = 333 = 93 = 27 = 4444 = 1644 = 644 = 256 = 1 Anything to the 0 power.
PRE-ALGEBRA. Lesson 4-7 Warm-Up PRE-ALGEBRA How do you multiply numbers with the same base? How do you multiply powers in algebraic expressions? Rule:
1.2 – Evaluate and Simplify Algebraic Expressions A numerical expression consists of numbers, operations, and grouping symbols. An expression formed by.
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia The Evolution of PLs1.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Exponents An exponent is the number of times the base is multiplied by itself. Example 27 can also be written as 3 This means 3 X 3 X 3.
Multiplication Properties of Exponents Multiplying with Like Bases and Exponents Keep the base the same and add the exponents. Ex: 3 2  3 7 = 3 9 x 4.
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.
1/38. 2/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
8-1 Adding and Subtracting Polynomials. Degree of a monomial: is the sum of the exponents of its variables. The degree of a nonzero constant is 0. Zero.
Identity and Equality Properties 1-4. Additive Identity The sum of any number and 0 is equal to the number. Symbols: a + 0 = a Example: 10 + n = 10 Solution:
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
Grade 6. Expression: a set of numbers that are related to one another by the use of operator symbols that represent a mathematical situation Has no equal.
Combining Like Terms and the Distributive Property.
Techniques of Differentiation. We now have a shortcut to find a derivative of a simple function. You multiply the exponent by any coefficient in front.
Opener Evaluate when x = 4.. Test Review Simplifying Exponent Rules.
Simplify Radical Expressions Using Properties of Radicals.
LESSON 4-7 EXPONENTS & MULTIPLYING. When we multiply terms with exponents  ADD exponents of like variables.
Degrees of a Monomial. Degree of a monomial: Degree is the exponent that corresponds to the variable. Examples: 32d -2x 4 16x 3 y 2 4a 4 b 2 c 44 has.
= 91 sum operation augend addend NOTE: Sometimes both the augend and addend are called addends. Sometimes the sum is called the total. Addition.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Exponents and Monomials. Monomial is an expression that is a number, a variable, or a product of a number and variables. Constant is a monomial containing.
Unit 2 Lesson #1 Derivatives 1 Interpretations of the Derivative 1. As the slope of a tangent line to a curve. 2. As a rate of change. The (instantaneous)
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
2 Understanding Variables and Solving Equations.
Section 5.2 The Integers.
Systems of Linear Equations
Differential Equations
Tagged Data Tag: a symbol in a data structure that identifies its type
Combining Like Terms and using the Distributive Property
6.001 SICP Object Oriented Programming
Basic Expressions Review
The number of times a number or expression (called a base) is used as a factor of repeated multiplication. Also called the power. 5⁴= 5 X 5 X 5 X 5 = 625.
Exponents and Monomials
Rational and Irrational Numbers and Their Properties (1.1.2)
6.001: Structure and Interpretation of Computer Programs
2-4 The Distributive Property
MTH1170 Differential Equations
Proving Properties of Recursive Functions and Data Structures
Unit 2 Expressions and Equations
Exponents and Monomials Lesson
Sec 3.6: DERIVATIVES OF LOGARITHMIC FUNCTIONS
Exponents and Monomials
Lecture #9 מבוא מורחב.
Objective Use multiplication properties of exponents to evaluate and simplify expressions.
1-9 Order of operations and distributive property
Simplifying Variable Expressions
Exponents and Monomials
2.7 The Distributive Property
1.2 Distributive Property & Combining Like Terms
Exponents and Monomials
list data list 만들기 list 사용하기 nil : list link :  * list -> list
6.2 Multiplying Powers with the Same Base
Presentation transcript:

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))))

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"

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)))))))

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

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

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

(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))))))

(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>)))

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

(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))))

(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))))

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

(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)))))

(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)))

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

(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))

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))