Presentation is loading. Please wait.

Presentation is loading. Please wait.

PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.

Similar presentations


Presentation on theme: "PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban."— Presentation transcript:

1 PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban

2 Scope and binding 2

3 3 Block Structure Lets write a procedure that given x, y, and z computes f(x,y,z) = (x+y) 2 + (x+z) 2 (define (sum-and-square x y) (square (+ x y))) (define (f x y z) (+ (sum-and-square x y) (sum-and-square x z)))

4 4 Block structure (cont.) (define (f x y z) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) Lets write a procedure that given inputs x, y, and z, computes f(x,y,z) = (x+y) 2 + (x+z) 2 while keeping sum-and-square private to f (hidden from the outside world): We discourage the use of define inside a function, and will later see a better way (LET) of hiding sum-and- square!

5 5 We still need to clarify the substitution model.. (define (f x y z) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) ==> (f 1 2 3) (define (sum-and-square 1 2) (square (+ 1 2))) (+ (sum-and-square 1 2) (sum-and-square 1 3)))

6 6 Bounded variables and scope A procedure definition binds its formal parameters The scope of the formal parameter is the body of the procedure. This is called lexical scoping (define (f x y z) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) x,y,z x,y

7 7 Evaluation of An Expression (refined) To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters replaced by the corresponding actual values. Do not substitute for occurrences that are bound by an internal definition. To Evaluate a combination: (other than special form) a.Evaluate all of the sub-expressions in any order b.Apply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions) The value of a numeral: number The value of a built-in operator: machine instructions to execute The value of any name: the associated object in the environment

8 8 The refined substitution model (define (f x y z) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) ==> (f 1 2 3) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square 1 2) (sum-and-square 1 3)))

9 9 The refined substitution model ==> (f 1 2 3) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square 1 2) (sum-and-square 1 3))) (+ (sum-and-square 1 2) (sum-and-square 1 3))) Sum-and-square Proc (x y) (square (+ x y))

10 (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define (good-enough? guess x) (< (abs (- (square guess) x)) precision)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt x) (sqrt-iter initial-guess x)) (define initial-guess 1.0) (define precision 0.0001)

11 Good programming Style 1. Divide the task to well-defined, natural, and simple sub-tasks. E.g: good-enough? and improve. Rule of thumb : If you can easily name it, it does a well-defined task. 2. Use parameters. E.g.: precision, initial-guess. 3. Use meaningful names.

12 Procedural abstraction It is better to: Export only what is needed Hide internal details. The procedure SQRT is of interest for the user. The procedure improve-guess is an internal detail. Exporting only what is needed leads to: A clear interface Avoids confusion

13 Rewriting SQRT (Block structure) (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess) x)) precision)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define initial-guess 1.0) (define precision 0.00001) (sqrt-iter initial-guess x))

14 Further improving sqrt Note that in every application of sqrt we substitute for x the same value in all subsequent applications of compound procedures ! Therefore we do not have to explicitly pass x as a formal variable to all procedures. Instead, can leave it unbounded (“free”).

15 SQRT again, taking advantage of the refined substitution model (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) precision)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (define initial-guess 1.0) (define precision 0.00001) (sqrt-iter initial-guess))

16 Lexical Scoping - again The lexical scoping rules means that the value of a variable which is unbounded (free) in a procedure f is taken from the procedure in which f was defined. It is also called static scoping

17 Another example for lexical scope (define (proc1 x) (define (proc2 y) (+ x y)) (define (proc3 x) (proc2 x)) (proc3 (* 2 x))) (proc1 4) proc1.x = 4 (proc3 8) proc3.x = 8 (proc2 8) proc2.y = 8 proc2.x=proc1.x=4 12

18 LET

19 For internal definitions, where possible we will prefer using let rather than define – Functional programming – Limits the scope of temporary definitions in a natural way Problematic when the internally defined function is recursive – Why? – To be discussed in the Practical Session

20 High-Order Procedures In functional programming (hence, in Scheme) procedures have a first class status: 1. Can be pointed to by variables. 2. Can be passed as arguments to procedures. 3. Can be returned as procedure values. 4. Can be included in data structures. 20

21 Example: sum-integers Signature: sum-integers(a,b) Purpose: to compute the sum of integers in the interval [a,b]. Type: [Number*Number -> Number] Post-conditions: result = a + (a+1) +... + b. Example: (sum-integers 1 5) should produce 15 Tests: (sum-integers 2 2) expected value: 2 (sum-integers 3 1) expected value: 0 (define sum-integers (lambda (a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b)))))

22 Example: sum-cubes Signature: sum-cubes(a,b) Purpose: to compute the sum of cubic powers of integers in the interval [a,b]. Type: [Number*Number -> Number] Post-conditions: result = a^3 + (a+1)^3 +... + b^3. (define sum-cubes (lambda (a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b)))))

23 Example: pi-sum Signature: pi-sum(a,b) Purpose: to compute the sum 1/(a*(a+2)) + 1/((a+4)*(a+6)) + 1/((a+8)*(a+10)) +... (which converges to PI/8, when started from a=1). Type: [Number*Number -> Number] Pre-conditions: if a 0. Post-conditions: result = 1/a*(a+2) + 1/(a+4)*(a+6) +... + 1/(a+4n)*(a+4n+2), s.t. a+4n b (define pi-sum (lambda (a b) (if (> a b) 0 (+ (/ 1 (* a (+ a 2))) (pi-sum (+ a 4) b)))))

24 Abstraction Signature: sum(term,a,next,b) Purpose: to compute the sum of terms, defined by in predefined gaps, defined by, in the interval [a,b]. Type: [[Number -> Number]*Number*[Number -> Number]*Number -> Number] Post-conditions: result = (term a) + (term (next a)) +... (term n), where n = (next (next...(next a))) <= b (define sum (lambda (term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))))

25 Using the abstracted form (define sum-integers (lambda (a b) (sum (lambda (x) x) a (lambda (x) (+ x 1)) b))) (define sum-cubes (lambda (a b) (sum cube a (lambda (x) (+ x 1)) b))) (define pi-sum (lambda (a b) (sum pi-term a pi-next b))) (define pi-term (lambda (x) (/ 1 (* x (+ x 2))))) (define pi-next (lambda (x) (+ x 4)))

26 Advantages Code reuse – Easier maintenance, understanding, debugging… General interface – Expresses a well-defined concept – Allows for further abstraction (define dx 0.005) (define integral (lambda (f a b) (* (sum f (+ a (/ dx 2)) add-dx b) dx))) (define add-dx (lambda (x) (+ x dx)))

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

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

29 29 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.

30 30 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…

31 31 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.

32 32 Extracting the common pattern: average-damp (define (average-damp f) (lambda (x) (average x (f x)))) average-damp: (number  number)  (number  number) A procedure is fed as input A procedure is returned ((average-damp square) 10) ((lambda (x) (average x (square x))) 10) (average 10 (square 10)) 55

33 (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) 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))

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

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

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


Download ppt "PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban."

Similar presentations


Ads by Google