מבוא מורחב 1 your turn (review) what does a lambda expression return when it is evaluated?...
TRANSCRIPT
מבוא מורחב1
Your turn (Review)
• What does a lambda expression return when it is evaluated?• the value of a lambda expression is a procedure
• What three things are in the code of every recursive algorithm?
• a recursive case a base case a test
• Which of the following is a use of lambda?1. (define fred +)2. (define (fred x) (+ x x))
• only 2: (define fred (lambda (x) (+ x x)))
מבוא מורחב2
(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)))
מבוא מורחב3
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))
4
Binding names קשירת שמות
• A procedure F binds a name x if either
1. x is a formal parameter of F
2. x is a name defined internally in F
(define (F x y z)
(define (h y) (* y y)
(+ (h x) (h y) (h z)))
• h, x, y, z are bound in F
• y is bound in h
מבוא מורחב5
עידון מודל ההצבה
When replacing a formal parameter by the corresponding argument, do not substitute for occurences that are bound by an internal definition.
An occurrence of a name is bound by the innermost procedure that binds the name and contains the occurrence in its body.
מבוא מורחב6
refining the substitution model
(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))
(sqrt 2)
מבוא מורחב7
(sqrt 2)
(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 2)
מבוא מורחב8
Scheme uses Lexical Scoping
מקום נקבע על פי F ערך משתנה חופשי בפונקציה •)F(ולא על פי מקןם הפעלת . Fהגדרת
תקבע את ערכו. F פונקציה המכילה את הגדרת •
מבוא מורחב9
(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))
מבוא מורחב10
Taking advantage of 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))
(sqrt 2)
מבוא מורחב11
(sqrt 2)
(define (good-enough? guess) (< (abs (- (square guess) 2)) 0.001)) (define (improve guess) (average guess (/ 2 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)))
מבוא מורחב12
(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
מבוא מורחב13
Applicative 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
מבוא מורחב14
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
מבוא מורחב15
Applicative
((lambda (x) (+ x x)) (* 3 4))
((lambda (x) (+ x x)) 12)
(+ 12 12)
24
Normal
((lambda (x) (+ x x)) (* 3 4))
(+ (* 3 4) (* 3 4))
(+ 12 12)
24
This may matter in some cases:
((lambda (x y) (+ x 2)) 3 (/ 1 0))
מבוא מורחב16
Compute ab
•(exp-1 2 2) ==> 4• wishful thinking :
• base case :
a * a(b-1)
a0 = 1
(define exp-1 (lambda (a b)
(if (= b 0) 1 (* a (exp-1 a (- b 1))))))
מבוא מורחב17
exp-1 is a recursive algorithm
• In a recursive algorithm, bigger operands => more space (define exp-1
(lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))))(fact 4)
(exp-1 2 3)(if (= 3 0) 1 (* 2 (exp-1 2 (- 3 1))))(* 2 (exp-1 2 2))..(* 2 (* 2 (exp-1 2 1)))..(* 2 (* 2 (* 2 (exp-1 2 0))))
• An iterative algorithm uses constant space
מבוא מורחב18
Intuition for iterative ab
• same as you would do if calculating 24 by hand:
1. start with 1 (4 more to go)
1. multiply 1 by 2 gives 2 (3 more to go)
2. multiply 2 by 2 gives 4 (2 more to go)
3. multiply 4 by 2 gives 8 (1 more to go)
4. multiply 8 by 2 gives 16 (0 more to go)
• At each step, only need to remember:
current product, how many times has yet to multiply
• Therefore, constant space
מבוא מורחב19
In scheme:
(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)
מבוא מורחב20
A trace
(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-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
21
Recursive = pending operations when procedure calls itself
• Recursive exponentiation:(define exp-1 (lambda (a b)
(if (= b 0) 1 (* a (exp-1 a (- b 1))))))
(exp-1 2 3) (if (= 3 0) 1 (* 2 (exp-1 2 (- 3 1)))) (* 2 (exp-1 2 2))
.. (* 2 (* 2 (exp-1 2 1)))
.. (* 2 (* 2 (* 2 (exp-1 2 0))))(fact 4)
operation pending
• Pending ops make the expression grow continuously
22
Iterative = no pending operations
• Iterative exponentiation: (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-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
• Fixed size because no pending operations
no pending operations
מבוא מורחב23
Summary
• Iterative algorithms have constant space• Using substitution the expression doesn’t grow.• Iterative algorithms have no pending operations
when the procedure calls itself
• How to develop an iterative algorithm• figure out a way to accumulate partial answers• translate rules into scheme code
מבוא מורחב25
Orders of growth of processes
• Suppose n is a parameter that measures the size of a problem
• Let R(n) be the amount of resources needed to compute a 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.
• 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 inputby a factor of 2, the resource consumption increase by a factor of 4
מבוא מורחב26
Orders of growth of processes
• We say R(n) has order of growth f(n)if there are constants k1 and k2 such that k1f(n)<= R(n)<= k2f(n) for all n
• R(n) O(f(n)) if there is a constant k such that
R(n) <= k f(n) for all n
• R(n) (f(n)) if there is a constant k such that
k f(n) <= R(n) for all n
x
x100n2 O(n)
100n2 (n)
100n2 (n)
100n2 (n2)
k1, k2, k 0
מבוא מורחב27
Resources consumed by exp-1
• In a recursive algorithm, bigger operands => more space (define exp-1
(lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))))(fact 4)
(exp-1 2 3)(if (= 3 0) 1 (* 2 (exp-1 2 (- 3 1))))(* 2 (exp-1 2 2))..(* 2 (* 2 (exp-1 2 1)))..(* 2 (* 2 (* 2 (exp-1 2 0))))
• Space b • Time b
מבוא מורחב28
Resources consumed by exp-2
(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-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
• Space 1 • Time b