Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.

Similar presentations


Presentation on theme: "Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5."— Presentation transcript:

1 Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5

2 Types so far Numbers: 1, 7, 1.2 Boolean: #t , #f
Strings: “this is a string” Procedures: + (lambda (x) (if (< x 0) “x is negative” “x is not negative”)) מבוא מורחב - שיעור 5

3 Procedures have types A procedure may have requirements regarding the
number of its arguments, may expect each argument to be of a certain type. The procedure + expects numbers as its arguments. Cannot be applied to strings. (+ “abc” “xyz”) מבוא מורחב - שיעור 5

4 Procedures have types The type of a procedure is (part of) a contract:
If the operands have the specified types, the procedure will result in a value of the specified type otherwise, its behavior is undefined maybe an error, maybe random behavior A contract between the caller and the procedure. caller responsible for argument number and types procedure responsible to deliver correct result מבוא מורחב - שיעור 5

5 number × number  number
Example The type of the procedure add: (define (add x y) (+ x y)) is: two arguments, both numbers result value of number addition is a number number × number  number (+ 7 “xx”) - causes an error. מבוא מורחב - שיעור 5

6 number × number × number number
Your turn The following expressions evaluate to values of what type? (lambda (a b c) (if (> a 0) (+ b c) (- b c))) number × number × number number (lambda (p) (if p "hi" "bye")) Boolean string (* 3.14 (* 2 5)) number (lambda (p) (if p "hi" 0)) ?? מבוא מורחב - שיעור 5

7 Types (summary) type: a set of possible values and operations
every value has a type procedure types (types which include ) indicate number of arguments required type of each argument type of result of the procedure מבוא מורחב - שיעור 5

8 Can procedures get and return procedures?
In scheme a procedure can: Return a procedure as its return value, Receive procedures as arguments. Why is this useful? מבוא מורחב - שיעור 5

9 Consider the following three sums
… = (100 * 101)/2 … = (100 * 101 * 102)/6 1 + 1/32 + 1/52 + … + 1/1012 ~ p2/8 In mathematics they are all captured by the notion of a sum: מבוא מורחב - שיעור 5

10 Let’s have a look at the three programs
(define (pi-sum a b) (if (> a b) (+ (/ 1 (square a)) (pi-sum (+ a 2) b)))) (define (sum-integers a b) (if (> a b) (+ a (sum-integers (+ 1 a) b)))) (define (sum-squares a b) (if (> a b) (+ (square a) (sum-squares (+ 1 a) b)))) (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) מבוא מורחב - שיעור 5

11 Let’s check this new procedure out!
(define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) What is the type of this procedure? ((number  number) × number × (number number) × number)  number procedure procedure procedure מבוא מורחב - שיעור 5

12 Higher-order procedures
(define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) Examples of use: 1. (define (sum-integers1 a b) (sum (lambda (x) x) a (lambda (x) (+ x 1)) b)) 2. (define (sum-squares1 a b) (sum square a (lambda (x) (+ x 1)) b)) 3. (define (pi-sum1 a b) (sum (lambda (x) (/ 1 (square x))) a (lambda (x) (+ x 2)) b)) A higher-order procedure: takes one or more procedures as arguments and/or returns one as a value מבוא מורחב - שיעור 5

13 … How does it work? (define (sum term a next b) (if (> a b) 0
(+ (term a) (sum term (next a) next b)))) (sum square 1 (lambda (x) (+ x 1)) 100) (+ (square 1) (sum square ((lambda (x) (+ x 1)) 1) (lambda (x) (+ x 1)) 100)) (+ 1 (sum square 2 (lambda (x) (+ x 1)) 100)) (+ 1 (+ (square 2) (sum square 3 (lambda (x) (+ x 1)) 100))) (+ 1 (+ 4 (sum square 3 (lambda (x) (+ x 1)) 100))) (+ 1 (+ 4 (+ 9 (sum square 4 (lambda (x) (+ x 1)) 100))) מבוא מורחב - שיעור 5

14 Integration as a procedure
Integration under a curve f is approximated by dx (f(a) + f(a + dx) + f(a + 2dx) + … + f(b)) a b dx f (define (integral f a b) (define dx 1.0e-3) (* (sum f a (lambda (x) (+ x dx)) b) dx)) arctan(a) = ∫(1/(1+y2))dy a (define atan (lambda (a) (integral (lambda (y) (/ 1 (+ 1 (square y)))) 0 a))) מבוא מורחב - שיעור 5

15 The derivative. We want to write a procedure with:
Argument: a function f: number  number Result: the function f’: number  number deriv: (number  number)  (number  number) (define (deriv f) (lambda (x) (define dx 0.001) (/ (- (f (+ x dx)) (f x)) dx))) > ((deriv square) 3) מבוא מורחב - שיעור 5

16 We will soon see more examples of higher-order procedures
Before that, LET us have an intermezzo מבוא מורחב - שיעור 5

17 The syntactic sugar “Let”
Suppose we wish to implement the function f(x,y) = x(1+x*y)2 + y(1-y) + (1+x*y)(1-y) We can also express this as a = 1+x*y b = 1-y f(x,y) = xa2 + yb + ab a b מבוא מורחב - שיעור 5

18 The syntactic sugar “Let”
(define (f x y) (define (f-helper a b) (+ (* x (square a)) (* y b) (* a b))) (f-helper (+ 1 (* x y)) (- 1 y))) (define (f x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y))) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b)))) מבוא מורחב - שיעור 5

19 The syntactic sugar “Let”
(Let ((<var1> <exp1>) (<var2> <exp2>) .. (<varn> <expn>)) <body>) bindings Is defined to be equivalent to: ((lambda (<var1> ….. <varn>) <body>) <exp1> <exp2> <expn>) מבוא מורחב - שיעור 5

20 More Procedural Abstraction… Fixed Points
x0 is a fixed point of F(x) if F(x0) = x0 x0 = Example: a is a fixed point of F(x) = a/x

21 Finding fixed points for f(x)
Start with an arbitrary first guess x1 Each time: try the guess, f(x) ~ x ?? If it’s not a good guess try the next guess xi+1 = f(xi) (define (fixed-point f first-guess) (define tolerance ) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) guess (try next)))) (try first-guess)) מבוא מורחב - שיעור 5

22 An example: f(x) = 1+1/x (define (f x) (+ 1 (/ 1 x)))
(fixed-point f 1.0) X1 = 1.0 X2 = f(x1) = 2 X3 = f(x2) = 1.5 X4 = f(x3) = X5 = f(x4) = 1.6 X6 = f(x5) = 1.625 X7 = f(x6) = … Exact fixed-point: … Note how odd guesses underestimate And even guesses Overestimate. מבוא מורחב - שיעור 5

23 Another example: f(x) = 2/x
(define (f x) (/ 2 x)) (fixed-point f 1.0) x1 = 1.0 x2 = f(x1) = 2 x3 = f(x2) = 1 x4 = f(x3) = 2 x5 = f(x4) = 1 x6 = f(x5) = 2 x7 = f(x6) = 1 Exact fixed-point: … מבוא מורחב - שיעור 5

24 How do we deal with oscillation?
Consider f(x)=2/x. If guess is a number such that guess < sqrt(2) then 2/guess > sqrt(2) So the average of guess and 2/guess is always an even Better guess. So, we will try to find a fixed point of g(x)= (x + f(x))/2 Notice that g(x) = (x +f(x)) /2 has the same fixed points as f. For f(x)=2/x this gives: g(x)= (x + 2/x)/2 מבוא מורחב - שיעור 5

25 To find an approximation of x: Make a guess G
Improve the guess by averaging G and x/G Keep improving the guess until it is good enough X = 2 G = 1 X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = X/G = 24/17 G = ½ (17/ /17) = 577/408 = מבוא מורחב - שיעור 5

26 Extracting the common pattern: average-damp
(define (average-damp f) ;outputs g(x)=(x+f(x))/2 (lambda (x) (average x (f x)))) average-damp: (number  number)  (number  number) ((average-damp square) 10) ((lambda (x) (average x (square x))) 10) (average 10 (square 10)) 55 מבוא מורחב - שיעור 5

27 … which gives us a clean version of sqrt
(define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) Compare this to our previous implementation of sqrt – same process. For the cubic root of x, fixed point of f(y) = x/y2 (define (cubert x) (average-damp (lambda (y) (/ x (square y)))) מבוא מורחב - שיעור 5

28 Further abstraction (define (osc-fixed-point f first-guess)
(fixed-point (average-damp f) first-guess)) (define (sqrt x) (osc-fixed-point (lambda (y) (/ x y)) 1.0) (define (cubert x) (osc-fixed-point (lambda (y) (/ x (square y))) מבוא מורחב - שיעור 5

29 A solution to the equation: F(x) = 0
Newton’s method A solution to the equation: F(x) = 0 is a fixed point of: G(x) = x - F(x)/F’(x) (define (newton-transform f) (lambda (x) (- x (/ (f x) ((deriv f) x))))) (define (newton-method f guess) (fixed-point (newton-transform f) guess)) (define (sqrt x) (newton-method (lambda (y) (- (square y) x)) 1.0)) מבוא מורחב - שיעור 5

30 Further abstraction (define (fixed-point-of-transform f transform guess) (fixed-point (transform f) guess)) (define (osc-fixed-point f guess) (fixed-point-of-transform f average-damp guess)) (define (newton-method f guess) newton-transform מבוא מורחב - שיעור 5


Download ppt "Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5."

Similar presentations


Ads by Google