Lecture 2 Lecturer Prof Benny Chor Office hours
Lecture 2 Lecturer: Prof. Benny Chor Office hours: Thursday 15 -16, Shrieber 223 benny@cs. tau. ac. il מבוא מורחב 1
Lecture Overview Refined substitution model of evaluation Recursive procedures Procedural abstraction Scope of Variables מבוא מורחב 2
Reminder: Syntax vs. semantics Syntax Primitives Semantics 23 + * 23 Means of Combination (+ 3 17 5) Application of proc to arguments Result = 25 Means of Abstraction (define score 23) Associates score with 23 in environment table Proc for adding Proc for multiplying מבוא מורחב 3
Reminder: Evaluation of An Expression 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) a. Evaluate all of the sub-expressions in any order b. Apply the procedure that is the value of the leftmost subexpression to the arguments (the values of the other subexpressions) מבוא מורחב 4
An example ==> (define score 23) ==> (* (+ * + 5 6 ) (- 5 6 - score (* 2 3 2 ))) 23 * 2 3 2 11 121 מבוא מורחב 5
Reminder: Lambda special form lambda: (lambda (x y) (+ x y x 2)) • 1 st operand position: the parameter list (x y) • a list of names (perhaps empty) • 2 nd operand position: the body (+ x y x 2) • may be any sequence of expressions The value of a lambda expression is a compound procedure. מבוא מורחב 6
Naming procedures 1. An application of an unnamed procedure: ((lambda (x) (* x x)) 4) ==> 16 2. Naming the procedure: • (define square (lambda (x) (* x x))) • (square 3) 7
Reminder: Abstraction for Compound procedures (define square (lambda (x) (* x x))) Environment Table Name square score Value 23 Proc (x) (* x x) מבוא מורחב 8
Some examples: • (define twice (lambda (x) (* 2 x)) ) • (twice 2) ==> 4 (twice 3) ==> 6 Using “syntactic sugar”: (define (twice x) (* 2 x)) • (define second (lambda (x y z) y) ) • (second 2 15 3) ==> 15 (second 34 -5 16) ==> -5 (define (second x y z) y) מבוא מורחב 9
New: Substitution model for Evaluation Lets evaluate: ==> (square 4) Proc (x) (* x x) 4 To Apply a compound procedure to a list of arguments: • Replace the formal parameters with the corresponding actual values. • Evaluate the body of the procedure with these values. (* 4 4) 16 מבוא מורחב 10
An example of the substitution model (define square (lambda (x) (* x x))) (define average (lambda (x y) (/ (+ x y) 2))) (average 5 (square 3)) (average 5 (* 3 3)) (average 5 9) first evaluate operands, then substitute (/ (+ 5 9) 2) (/ 14 2) 7 if operator is a primitive procedure, replace by result of operation מבוא מורחב 11
Evaluation of An Expression (refined) 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) a. Evaluate all of the sub-expressions in any order b. Apply the procedure that is the value of the leftmost subexpression to the arguments (the values of the other subexpressions) 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 מבוא מורחב 12
Values and types In scheme almost every expression has a value Examples: 1) The value of 23 is 23 2) The value of (lambda (x) (* x x)) is the compound procedure Proc (x) (* x x) Values have types. For example: 1) The type of 23 is numeral 2) The type of Proc (x) (* x x) is a compound procedure מבוא מורחב 13
Reminder: Booleans Two distinguished values denoted by the constants #t and #f The type of these values is boolean ==> (< 2 3) #t ==> (< 4 3) #f מבוא מורחב 14
Reminder: IF special form (if <predicate> <consequent> <alternative>) (if (< 2 3) ==> 2 (if (< 2 3) 2 (/ 1 0)) ==> ERROR 2 If the value of <predicate> is #t, Evaluate <consequent> and return it Otherwise Evaluate <alternative> and return it In a “regular” form, we first evaluate all arguments and then apply the function מבוא מורחב 15
Sum of squares • S(n) = 02 + 12 + 22 ………. …… (n-1)2 + n 2 S(n-1) Notice that: • S(n) = S(n-1) + n 2 • S(0) = 0 These two properties completely define the function Wishful thinking: if I could only solve the smaller instance … 16
Recursive algorithm for sum of squares (define sum-squares (lambda (n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n)))) 17
General form of recursive algorithms • test, base case, recursive case (define sum-sq (lambda (n) (if (= n 0) ; test for base case 0 ; base case (+ (sum-sq (- n 1)) (square n)) ; recursive case ))) • base case: non-decomposable problem • recursive case: larger (decomposable) problem מבוא מורחב 18
Evaluating (sum-squares 3) (define (sum-squares n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n)))) (sum-squares 3) (if (= 3 0) 0 (+ (sum-squares (- 3 1)) (square 3))) (+ (sum-squares (- 3 1)) (square 3)) (+ (sum-squares (- 3 1)) (* 3 3)) (+ (sum-squares (- 3 1)) 9) (+ (sum-squares 2) 9) (+ (if (= 2 0) 0 (+ (sum-squares (- 2 1)) (square 2))) 9) … (+ (+ (sum-squares 1) 4) 9) … (+ (+ (+ (sum-squares 0) 1) 4) 9) (+ (+ (+ (if (= 0 0) 0 (+ (sum-squares (- 0 1)) (square 0))) 1) 4) 9) (+ (+ (+ 0 1) 4) 9) … 14 What would have happen if ‘if’ was a regular function ? מבוא מורחב 19
Another example of a recursive algorithm • even? (define even? (lambda (n) (if (= n 0) #t ; test for base case ; base case (not (even? (- n 1))) ; recursive case ))) מבוא מורחב 21
Short summary • Design a recursive algorithm by 1. Solving big instances using the solution to smaller instances. 2. Solving directly the base cases. • Recursive algorithms have 1. test 2. recursive case 3. base case מבוא מורחב 22
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))) מבוא מורחב 23
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))) מבוא מורחב 24
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))) מבוא מורחב 25
Bounded variables and scope A procedure definition binds its formal parameters The scope of the formal parameter is the body of the procedure. (define (f x y z) (define (sum-and-square x y) (square (+ x y))) x, y, z x, y (+ (sum-and-square x y) (sum-and-square x z))) מבוא מורחב 26
Evaluation of An Expression (refined) 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) a. Evaluate all of the sub-expressions in any order b. Apply the procedure that is the value of the leftmost subexpression to the arguments (the values of the other subexpressions) 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. מבוא מורחב 27
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))) מבוא מורחב 28
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 Proc (x y) (square (+ x y)) (+ (sum-and-square 1 2) (sum-and-square 1 3))) מבוא מורחב 29
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 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 מבוא מורחב 30
(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)) מבוא מורחב 31
Good programming Style 1. Divide the task to well-defined, natural, and simple sub-tasks. E. g: good-enough? and improve. Thumb of rule: If you can easily name it, it does a well-defined task. 2. Use parameters. E. g. : precision, initial-guess. 3. Use meaningful names. מבוא מורחב 32
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 מבוא מורחב 33
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) (sqrt-iter initial-guess x)) מבוא מורחב 34
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”). מבוא מורחב 35
Lexical Scoping The value of an variable which is unbounded (free) in a procedure f is taken from the procedure in which f was defined. מבוא מורחב 36
SQRT again, take 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)) מבוא מורחב 37
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)) 38
Another example (define (proc 1 x) (define (proc 2 y) (+ x y)) (define (proc 3 x) (proc 2 x)) (proc 3 (* 2 x))) Proc 1. x Proc 3. x (proc 1 4) (proc 3 8) (proc 2 8) 12 proc 1. x = 4 proc 3. x = 8 proc 2. y = 8 מבוא מורחב proc 2. x=proc 1. x=4 39
- Slides: 38