3.14""> 3.14"">
Download presentation
Presentation is loading. Please wait.
Published byAdam Robertson Modified over 9 years ago
1
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special forms (define pi 3.14) syntax combination: (oper-expression other-expressions …) special form:special keyword as first subexpression semantics combinations:evaluate subexpressions in any order apply operator to operands special forms:each one special
2
מבוא מורחב 2 Read-Eval-Print-Loop: two worlds visible world execution world 23 eval self-rule print expression value printed representation of value pi 3.14 eval name-rule print value name-rule: look up value of name in current environment
3
מבוא מורחב 3 Review: define special form define-rule: evaluate 2nd operand only name in 1st operand position is bound to that value overall value of the define expression is undefined (define pi 3.14) eval define-rule undefined print scheme versions differ visible world execution world namevalue pi3.14 "pi --> 3.14"
4
מבוא מורחב 4 Mathematical operators are just names 1.(+ 3 5)==> 8 2.(define fred +) ==> undef 3.(fred 4 6)==> 10 How to explain this? Explanation: + is just a name + is bound to a value which is a procedure line 2 binds the name fred to that same value
5
מבוא מורחב 5 Primitive procedures are just values visible world execution world * #[compiled-procedure 8 #x583363] eval name-rule print A primitive proc that multiplies its arguments expression value printed representation of value
6
מבוא מורחב 6 Lambda: making new procedures (lambda (x) (* x x)) expression eval lambda-rule A compound proc that squares its argument value #[compound-procedure 9] print printed representation of value
7
7 Interaction of define and lambda 1.(lambda (x) (* x x)) ==> #[compound-procedure 9] 2.(define square (lambda (x) (* x x))) ==> undef 3.(square 4)==> 16 4.((lambda (x) (* x x)) 4)==> 16 Syntactic Sugar: 5.(define (square x) (* x x)) ==> undef a convenient shorthand for 2 above this is a use of lambda!
8
מבוא מורחב 8 Lambda special form lambda syntax (lambda (x y) (/ (+ x y) 2)) 1st operand position: the parameter list (x y) a list of names (perhaps empty) determines the number of operands required 2nd operand position: the body (/ (+ x y) 2) may be any expression not evaluated when the lambda is evaluated evaluated when the procedure is applied semantics of lambda:
9
מבוא מורחב 9 THE VALUE OF A LAMBDA EXPRESSION IS A PROCEDURE
10
מבוא מורחב 10 Your turn: fill in each box (define twice ) (twice 2) ==> 4 (twice 3) ==> 6 (define constant2 (lambda () 2)) ==> 2 (define second ) (second 2 15 3) ==> 15 (second 34 -5 16) ==> -5 (lambda (x) (* 2 x)) (constant2) (lambda (x y z) y)
11
מבוא מורחב 11 Substitution model a way to figure out what happens during evaluation not really what happens in the computer to apply a compound procedure: evaluate the body of the procedure, with each parameter replaced by the corresponding operand to apply a primitive procedure: just do it (define square (lambda (x) (* x x))) 1. (square 4) 2. (* 4 4) 3.16
12
מבוא מורחב 12 Substitution model details (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 (applicative order) (/ (+ 5 9) 2) (/ 14 2) if operator is a primitive procedure, 7 replace by result of operation
13
13 A less trivial procedure: sum of squares S(n) = 0 2 + 1 2 + 2 2 ………. …… + n 2 Notice that S(n) = S(n-1) + n 2 Also notice that S(0) = 0 (define sum-sq (lambda (n) (if (= n 0) 0 (+ (sum-sq (- n 1)) (square n)))) predicate =tests numerical equality (= 4 4) ==> #t (true) (= 4 5) ==> #f (false) if special form (if (= 4 4) 2 3) ==> 2 (if (= 4 5) 2 3) ==> 3 predicateconsequent alternative
14
מבוא מורחב 14 IF special form (if ) When the value of is #f, the value of the expression is the value of. Otherwise it is the value of 1 #t (if ( (if ( 9 8 7)) ==>
15
מבוא מורחב 15 (sum-sq 3) (if (= 3 0) 0 (+ (sum-sq (- 3 1)) (square 3))) (if #f 0 (+ (sum-sq (- 3 1)) (square 3))) (+ (sum-sq (- 3 1)) (square 3)) (+ (sum-sq (- 3 1)) (* 3 3)) (+ (sum-sq (- 3 1)) 9) (+ (sum-sq 2) 9) (+ (if (= 2 0) 0 (+ (sum-sq (- 2 1)) (square 2))) 9).. (+ (+ (sum-sq 1) 4) 9).. (+ (+ (+ (sum-sq 0) 1) 4) 9) (+ (+ (+ (if (= 0 0) 0(+ (sum-sq (- 0 1)) (square 0))) 1) 4) 9).. (+ (+ (+ 0 1) 4) 9).. 14 (define (sum-sq n) (if (= n 0) 0 (+ (sum-sq (- n 1)) (square n))))
16
מבוא מורחב 16 How to design recursive algorithms follow the general pattern: 1. wishful thinking 2. decompose the problem 3. identify non-decomposable (smallest) problems 1. Wishful thinking Assume the desired procedure exists. want to implement sum-sq ? OK, assume it exists. BUT, only solves a smaller version of the problem.
17
מבוא מורחב 17 2. Decompose the problem Solve a problem by 1. solve a smaller instance (using wishful thinking) 2. convert that solution to the desired solution Step 2 requires creativity! Must design the strategy before coding. S(n) = S(n-1) + n 2 solve the smaller instance, add n 2 to get the solution (define sum-sq (lambda (n) (+ (sum-sq(- n 1)) (square n))))
18
מבוא מורחב 18 3. Identify non-decomposable problems Decomposing not enough by itself Must identify the "smallest" problems and solve directly S(0)=0 (define sum-sq (lambda (n) (if (= n 0) 0 (+ (sum-sq (- n 1)) (square n)))))
19
מבוא מורחב 19 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: smallest (non-decomposable) problem recursive case: larger (decomposable) problem
20
מבוא מורחב 20 Another example odd? (define odd? (lambda (n) (not (odd? (- n 1))) ; recursive case ))) (if (= n 0) ; test for base case #f ; base case See another way to write it in the notes
21
מבוא מורחב 21 Short summary Design a recursive algorithm by 1. wishful thinking 2. decompose the problem 3. identify non-decomposable (smallest) problems Recursive algorithms have 1. test 2. recursive case 3. base case
22
מבוא מורחב 22 Procedural abstraction Abstractions hide complexity Several fundamental kinds of abstraction this term Procedural abstraction is one: (sum-sq n) computes S(n) user doesn't have to think about the internals
23
מבוא מורחב 23 Procedural abstraction (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 G = 1X = 2 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
24
מבוא מורחב 24 (define (sqrt x) (sqrt-iter 1.0 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)) 0.001)) (define (improve guess x) (average guess (/ x guess)))
25
מבוא מורחב 25 Block Structure (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (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))) (sqrt-iter 1.0 x))
26
מבוא מורחב 26 Lexical Scoping (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0))
27
מבוא מורחב 27 => (sqrt 4) (define (good-enough? guess) (< (abs (- (square guess) 4)) 0.001)) (define (improve guess) (average guess (/ 4 guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0) (if (good-enough? 1.0) 1.0 (sqrt-iter (improve 1.0)))
28
מבוא מורחב 28 (define (h x) (define (f y) (+ x y)) (define (g x) (f x)) (g (* 2 x))) => (h 1) (define (f y) (+ 1 y)) (define (g x) (f x)) (g (* 2 1)) (f 2) (+ 1 2) 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.