Download presentation
Presentation is loading. Please wait.
Published byReynard Townsend Modified over 8 years ago
1
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures
2
מבוא מורחב - שיעור 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”))
3
מבוא מורחב - שיעור 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”)
4
מבוא מורחב - שיעור 5 4 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 Procedures have types
5
מבוא מורחב - שיעור 5 5 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.
6
מבוא מורחב - שיעור 5 6 Your turn The following expressions evaluate to values of what type? (lambda (a b c) (if (> a 0) (+ b c) (- b c))) (lambda (p) (if p "hi" "bye")) (* 3.14 (* 2 5)) number × number × number number number Boolean string (lambda (p) (if p "hi" 0)) ??
7
מבוא מורחב - שיעור 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
8
מבוא מורחב - שיעור 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?
9
מבוא מורחב - שיעור 5 9 Consider the following three sums 1 + 2 + … + 100 = (100 * 101)/2 1 + 4 + 9 + … + 100 2 = (100 * 101 * 102)/6 1 + 1/3 2 + 1/5 2 + … + 1/101 2 ~ 2 /8 In mathematics they are all captured by the notion of a sum:
10
מבוא מורחב - שיעור 5 10 Let’s have a look at the three programs (define (pi-sum a b) (if (> a b) 0 (+ (/ 1 (square a)) (pi-sum (+ a 2) b)))) (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ 1 a) b)))) (define (sum-squares a b) (if (> a b) 0 (+ (square a) (sum-squares (+ 1 a) b)))) (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b))))
11
מבוא מורחב - שיעור 5 11 Let’s check this new procedure out! (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) What is the type of this procedure? procedure ((number number) × number × (number number) × number) number
12
מבוא מורחב - שיעור 5 12 Higher-order procedures 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)) (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) A higher-order procedure: takes one or more procedures as arguments and/or returns one as a value
13
מבוא מורחב - שיעור 5 13 How does it work? (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (+ 1 (+ 4 (sum square 3 (lambda (x) (+ x 1)) 100))) (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 (+ 9 (sum square 4 (lambda (x) (+ x 1)) 100))) …
14
מבוא מורחב - שיעור 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) ) (define (integral f a b) (define dx 1.0e-3) (* (sum f a (lambda (x) (+ x dx)) b) dx)) a b dx f (define atan (lambda (a) (integral (lambda (y) (/ 1 (+ 1 (square y)))) 0 a))) arctan(a) = ∫(1/(1+y 2 ))dy 0 a
15
מבוא מורחב - שיעור 5 15 The derivative. We want to write a procedure with: Argument: a function f: number number Result: the function f’: number number (define (deriv f) (lambda (x) (define dx 0.001) (/ (- (f (+ x dx)) (f x)) dx))) > ((deriv square) 3) 6.000999999999479 deriv: (number number) (number number)
16
מבוא מורחב - שיעור 5 16 We will soon see more examples of higher-order procedures Before that, LET us have an intermezzo
17
מבוא מורחב - שיעור 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) = xa 2 + yb + ab a a b b
18
מבוא מורחב - שיעור 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))))
19
מבוא מורחב - שיעור 5 19 The syntactic sugar “Let” (Let (( ) ( ).. ( )) ) ((lambda ( ….. ) ) … ) Is defined to be equivalent to: bindings
20
20 More Procedural Abstraction… Fixed Points x 0 is a fixed point of F(x) if F(x 0 ) = x 0 Example: x 0 = a is a fixed point of F(x) = a/x
21
מבוא מורחב - שיעור 5 21 Finding fixed points for f(x) Start with an arbitrary first guess x 1 Each time: try the guess, f(x) ~ x ?? If it’s not a good guess try the next guess x i+1 = f(x i ) (define (fixed-point f first-guess) (define tolerance 0.00001) (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))
22
מבוא מורחב - שיעור 5 22 An example: f(x) = 1+1/x (define (f x) (+ 1 (/ 1 x))) (fixed-point f 1.0) X 1 = 1.0 X 2 = f(x 1 ) = 2 X 3 = f(x 2 ) = 1.5 X 4 = f(x 3 ) = 1.666666666.. X 5 = f(x 4 ) = 1.6 X 6 = f(x 5 ) = 1.625 X 7 = f(x 6 ) = 1.6153846… Exact fixed-point: 1.6180339… Note how odd guesses underestimate And even guesses Overestimate.
23
מבוא מורחב - שיעור 5 23 Another example: f(x) = 2/x (define (f x) (/ 2 x)) (fixed-point f 1.0) x 1 = 1.0 x 2 = f(x 1 ) = 2 x 3 = f(x 2 ) = 1 x 4 = f(x 3 ) = 2 x 5 = f(x 4 ) = 1 x 6 = f(x 5 ) = 2 x 7 = f(x 6 ) = 1 Exact fixed-point: 1.414213562…
24
מבוא מורחב - שיעור 5 24 How do we deal with oscillation? Consider f(x)=2/x. If guess is a number such that 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 For f(x)=2/x this gives: g(x)= (x + 2/x)/2 Notice that g(x) = (x +f(x)) /2 has the same fixed points as f.
25
מבוא מורחב - שיעור 5 25 X = 2G = 1 X/G = 2G = ½ (1+ 2) = 1.5 X/G = 4/3G = ½ (3/2 + 4/3) = 17/12 = 1.416666 X/G = 24/17G = ½ (17/12 + 24/17) = 577/408 = 1.4142156 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
26
מבוא מורחב - שיעור 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
27
מבוא מורחב - שיעור 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/y 2 (define (cubert x) (fixed-point (average-damp (lambda (y) (/ x (square y)))) 1))
28
מבוא מורחב - שיעור 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))) 1.0)
29
מבוא מורחב - שיעור 5 29 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))
30
מבוא מורחב - שיעור 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) (fixed-point-of-transform f newton-transform guess))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.