1 / 40

Lecture 3

Lecture 3. Material in the textbook Sections 1.1.5 to 1.2.1. Review. Procedures capture common patterns Naming abstraction allows us to use them as primitive objects עוץ-לי-גוץ-לי Principle: “If you capture a process (in a procedure), and give it a name,

tawny
Download Presentation

Lecture 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3 Material in the textbook Sections 1.1.5 to 1.2.1 מבוא מורחב

  2. Review • Procedures capture common patterns • Naming abstraction allows us to use them as primitive objects • עוץ-לי-גוץ-לי Principle: “If you capture a process (in a procedure), and give it a name, you have power over that process” • The Substitution Model מבוא מורחב

  3. 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 • To Evaluate a combination: (other than special form) • Evaluate all of the sub-expressions in any order • Apply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions) Evaluation of An Expression 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. מבוא מורחב

  4. Review: 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))) מבוא מורחב

  5. Block structure (cont.) We could also make sum-and-square private to f: (define (f x y z)(define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) Syntactically: (define (<name> <params>) <list of defs> <body>) מבוא מורחב

  6. (define (sum-and-square 1 2) (square (+ 1 2))) (+ (sum-and-square 1 2) (sum-and-square 1 3))) 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) מבוא מורחב

  7. (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 Bounded variables and scope A procedure definition binds its formal parameters The scope of the formal parameter is the body of the procedure. מבוא מורחב

  8. 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 • To Evaluate a combination: (other than special form) • Evaluate all of the sub-expressions in any order • Apply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions) 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 in an internal definition. מבוא מורחב

  9. (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square 1 2) (sum-and-square 1 3))) 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) מבוא מורחב

  10. X = 2 G = 1 X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = 1.416666 X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156 SQRT • To find an approximation of square root of x: • Make a guess G • Improve the guess by averaging G and x/G • Keep improving the guess until it is good enough מבוא מורחב

  11. Procedure SQRT (define initial-guess 1.0) (define precision 0.0001) (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)) מבוא מורחב

  12. sqrt-iter good-enough? improve average abs square Procedural decomposition of SQRT sqrt מבוא מורחב

  13. 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-guessis an internal detail. • Exporting only what is needed leads to: • A clear interface • Avoids confusion מבוא מורחב

  14. Rewriting SQRT (Block structure) (define (sqrt x) (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 initial-guess 1.0) (define precision 0.00001) The same x (sqrt-iter initial-guess x) ) מבוא מורחב

  15. SQRT (Cleaner) Drop X as a parameter since it has the same value as the global X (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) ) The order of the definitions does not matter מבוא מורחב

  16. SQRT ( Cont.) ==>(sqrt 2) (define (good-enough? guess) (< (abs (- (square guess) 2)) precision)) (define (improve guess) (average guess (/ 2 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))

  17. Today • Refine our model • Applicative vs. Normal Order • Understand how it captures the nature of processes • Recursive vs. Iterative processes • Orders of growth מבוא מורחב

  18. Applicative order vs. Normal OrderEvaluation To Evaluate a combination: (other than special form) Evaluate all of the sub-expressions in any order Applythe procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions) מבוא מורחב

  19. Applicative order evaluation rules Combination... (<operator> <operand1> …… <operand n>) • Evaluate <operator> to get the procedure and evaluate <operands> to get the arguments • If <operator> is primitive: do whatever magic it does • If <operator> is compound: evaluate body with formal parameters replaced by arguments מבוא מורחב

  20. Example: Factorial • wishful thinking : • base case: n! =n * (n-1)! n = 1 (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1)))))) מבוא מורחב

  21. Example: Factorial • ==>(fact 3) • ([[n](if(= n 1) 1 (* n (fact (- n 1))))] 3) • (if (= 3 1) 1 (* 3 (fact (- 3 1)))) • (if #f 1 (* 3 (fact (- 3 1)))) • (* 3 (fact (- 3 1))) • (* 3 ([[n](if(= n 1) 1 (* n (fact (- n 1))))] (- 3 1))) • (* 3 (if (= 2 1) 1 (* 2 (fact (- 2 1))))) • (* 3 (if #f 1 (* 2 (fact (- 2 1))))) • (* 3 (* 2 (fact (-2 1)))) • (* 3 (* 2 (([[n](if(= n 1) 1 (* n (fact (- n 1))))] (- 2 1)))) • (* 3 (* 2 (if (= 1 1) 1 (* (fact (- 1 1))))))) • (* 3 (* 2 (if #t 1 (* 1 (fact (- 1 1)))))) • (* 3 (* 2 1)) • (* 3 2) • 6 מבוא מורחב

  22. Normal order evaluation Combination … (<operator> <operand1> …… <operand n>) • Evaluate <operator> to get the procedure and evaluate <operands> to get the arguments • If <operator> is primitive: do whatever magic it does • If <operator> is compound: evaluate body with formal parameters replaced by arguments מבוא מורחב

  23. The Difference Normal ((lambda (x) (+ x x)) (* 3 4)) (+ (* 3 4) (* 3 4)) (+ 12 12) 24 Applicative ((lambda (x) (+ x x)) (* 3 4)) (+ 12 12) 24 This may matter in some cases: ((lambda (x y) (+ x 2)) 3 (/ 1 0)) Scheme is an Applicative Order Language! מבוא מורחב

  24. Compute ab (Recursive Approach) • wishful thinking : • base case: ab=a * a(b-1) a0 = 1 • (define exp-1 • (lambda (a b) • (if (= b 0) • 1 • (* a (exp-1 a (- b 1)))))) מבוא מורחב

  25. ab=a * a * a*…*a • Which is: b ab = a2 *a*…*a= a3 *…*a • Operationally: • Halting condition: product  product * a counter  counter - 1 counter = 0 Compute ab (Iterative Approach) • Another approach: מבוא מורחב

  26. Syntactic Recursion Compute ab (Iterative Approach) (define (exp-2 a b) (define (exp-iter a counter product) (if (= counter 0) product (exp-iter a (- counter 1) (* a product))) (exp-iter a b 1)) How then, do the two procedures differ? They give rise to different processes – lets use our model to understand how. מבוא מורחב

  27. Recursive Process • (define exp-1 • (lambda (a b) • (if (= b 0) • 1 • (* a (exp-1 a (- b 1)))))) • (exp-1 3 4) • (* 3 (exp-1 3 3)) • (* 3 (* 3 (exp-1 3 2))) • (* 3 (* 3 (* 3 (exp-1 3 1)))) • (* 3 (* 3 (* 3 (* 3 (exp-1 3 0))))) • (* 3 (* 3 (* 3 (* 3 1)))) • (* 3 (* 3 (* 3 3))) • (* 3 (* 3 9)) • (* 3 27) • 81 מבוא מורחב

  28. Iterative Process (define (exp-2 a b) (define (exp-iter a counter product) (if (= counter 0) product (exp-iter a (- counter 1) (* a product))) (exp-iter a b 1)) (exp-2 3 4) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter 3 1 27) (exp-iter 3 0 81) 81 מבוא מורחב

  29. (exp-1 3 4) • (* 3 (exp-1 3 3)) • (* 3 (* 3 (exp-1 3 2))) • (* 3 (* 3 (* 3 (exp-1 3 1)))) • (* 3 (* 3 (* 3 (* 3 (exp-1 3 0))))) • (* 3 (* 3 (* 3 (* 3 1)))) • (* 3 (* 3 (* 3 3))) • (* 3 (* 3 9)) • (* 3 27) • 81 (exp-2 3 4) Growing amount of space (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) Constant amount of space (exp-iter 3 1 27) (exp-iter 3 0 81) 81 The Difference מבוא מורחב

  30. operation pending no pending operations Why More Space? • Recursive exponentiation: (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))) • Iterative exponentiation: • (define (exp-2 a b) • (define (exp-iter a counter product) • (if (= counter 0) • product • (exp-iter a (- counter 1) (* a product)))) • (exp-iter a b 1))

  31. Summary • Recursive process num of deferred operations “grows proportional to b” • Iterative process num of deferred operations stays “constant” (actually it’s zero) Can we better quantify these observations? Orders of growth… מבוא מורחב

  32. Order of Growth: Recursive Process • (exp-1 3 4) • (* 3 (exp-1 3 3)) • (* 3 (* 3 (exp-1 3 2))) • (* 3 (* 3 (* 3 (exp-1 3 1)))) • (* 3 (* 3 (* 3 (* 3 (exp-1 3 0))))) • (* 3 (* 3 (* 3 (* 3 1)))) • (* 3 (* 3 (* 3 3))) • (* 3 (* 3 9)) • (* 3 27) • 81  4 • (exp-1 3 5) • (* 3 (exp-1 3 4)) • (* 3 (* 3 (exp-1 3 3))) • (* 3 (* 3 (* 3 (exp-1 3 2)))) Dependent on b • (* 3 (* 3 (* 3 (* 3 (exp-1 3 1))))) • (* 3 (* 3 (* 3 (* 3 (* 3 (exp-1 3 0))))) • (* 3 (* 3 (* 3 (* 3 (* 3 1)))) • (* 3 (* 3 (* 3 (* 3 3)))) • (* 3 (* 3 (* 3 9))) • (* 3 (* 3 27)) • (* 3 81)  5 • 243

  33. Iterative Process (define (exp-2 a b) (define (exp-iter a b product) (if (= b 0) product (exp-iter a (- b 1) (* a product)))) (exp-iter a b 1) (exp-2 3 4) (exp-2 3 5) (exp-iter 3 4 1) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter 3 2 9) (exp-iter 3 1 27) (exp-iter 3 2 27) (exp-iter 3 0 81) (exp-iter 3 0 81) (exp-iter 3 0 243) 81 243 Some constant, independent of b

  34. The worst-case over all inputs of size n Orders of Growth • Suppose n is a parameter that measures the size of a problem (the size of its input) • R(n)measures the amount of resources needed to compute a solution procedure of size n. • Two common resources are space, measured by the number of deferred operations, and time, measured by the number of primitive steps. מבוא מורחב

  35. Orders of Growth • Want to estimate the “order of growth” of R(n): R1(n)=100n2 R2(n)=2n2+10n+2 R3(n) = n2 Are all the same in the sense that if we multiply the input by a factor of 2, the resource consumption increases by a factor of 4 Order of growth is proportional to n2 מבוא מורחב

  36. Orders of Growth • We say R(n)has order of growth Q(f(n))if there are constants c1 0and c2 0 such that for all n c1f(n)<= R(n)<= c2f(n) • R(n)O(f(n)) if there is a constant c  0such that for all n • R(n) <= cf(n) • R(n)(f(n)) if there is a constant c  0such that for all n • cf(n)<= R(n) מבוא מורחב

  37. Orders of Growth True or False? f 100n2 O(n) t 100n2 (n) 100n2 Q(n) f 100n2 Q(n2) t True or False? f 2100 (n) t 2100n  O(n2) 2n  Q(n) f 210 Q(1) t מבוא מורחב

  38. (exp-1 3 4) “n”=b=4 • (* 3 (exp-1 3 3)) • (* 3 (* 3 (exp-1 3 2))) • (* 3 (* 3 (* 3 (exp-1 3 1)))) • (* 3 (* 3 (* 3 (* 3 (exp-1 3 0))))) • (* 3 (* 3 (* 3 (* 3 1)))) • (* 3 (* 3 (* 3 3))) • (* 3 (* 3 9)) • (* 3 27) • 81 Resources Consumed by EXP-1 • Space b <= R(b) <= b which is Q(b) • Time b <= R(b) <= 2b which is Q(b) Linear Recursive Process מבוא מורחב

  39. (exp-2 3 4) “n”=b=4 (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter 3 1 27) (exp-iter 3 0 81) 81 Resources Consumed by EXP-2 • Space Q(1) • Time Q(b) Linear Iterative Process מבוא מורחב

  40. Summary • Trying to capture the nature of processes • Quantify various properties of processes: • Number of steps a process takes (Time Complexity) • Amount of Space a process uses (Space Complexity) מבוא מורחב

More Related