Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Binding names קשירת שמות A occurrence of a name z is bound by the innermost procedure that contains the name and either 1. z is a formal parameter of.

Similar presentations


Presentation on theme: "1 Binding names קשירת שמות A occurrence of a name z is bound by the innermost procedure that contains the name and either 1. z is a formal parameter of."— Presentation transcript:

1 1 Binding names קשירת שמות A occurrence of a name z is bound by the innermost procedure that contains the name and either 1. z is a formal parameter of F 2. z is a name defined internally in F (define (F x y z) (define (h y) (* y y) (+ (h x) (h y) (h z)))

2 מבוא מורחב 2 עידון מודל ההצבה When you apply F replace only occurences bound by F of each formal parameter. Do not replace occurences bound by an internal definition!

3 מבוא מורחב 3 (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)) (g 2) (f 2) (+ 1 2) 3

4 מבוא מורחב 4 Scheme uses Lexical Scoping ערך משתנה חופשי בפונקציה F נקבע על פי מקום הגדרת F. (ולא על פי מקום הפעלתה: dynamic binding) פונקציה המכילה את הגדרת F תקבע את ערכו.

5 מבוא מורחב 5 Orders of growth of processes R(n)  O(f(n)) if there is a constant k such that R(n) <= k f(n) for all n (positive integer) R(n)   (f(n)) if there is a constant k such that k f(n) <= R(n) for all n, k  0 R(n)   (f(n)) if R(n)  O(f(n)) and R(n)   (f(n)) 100n 3 + 10n  O(n 3 ) 100n 3 + 10n   (n 3 ) 100n 3 + 10n   (n 3 ) 10*log(n) + loglog(n)   ( ) log(n)

6 מבוא מורחב 6 Factorial (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1)))))) (fact 4) (if (= 4 1) 1 (* 4 (fact (- 4 1)))) (* 4 (fact 3)) (* 4 (if (= 3 1) 1 (* 3 (fact (- 3 1))))) (* 4 (* 3 (fact 2))) (* 4 (* 3 (if (= 2 1) 1 (* 2 (fact (- 2 1)))))) (* 4 (* 3 (* 2 (fact 1)))) (* 4 (* 3 (* 2 (if (= 1 1) 1 (* 1 (fact (- 1 1))))))) (* 4 (* 3 (* 2 1))) (* 4 (* 3 2)) (* 4 6) 24

7 מבוא מורחב 7 Examples of orders of growth FACT Space S(n)   (n) – linear Time T(n)   (n) – linear T(n) = The time it takes for fact to compute n! in general: The maximum time it takes to compute the result for an input of size n.

8 8 Iterative factorial (define (ifact n) (define ifact-helper (lambda (product count n) (if (> count n) product (ifact-helper (* product count) (+ count 1) n)))) (ifact-helper 1 1 n)) (ifact 4) (ifact-helper 1 1 4) (if (> 1 4) 1 (ifact-helper (* 1 1) (+ 1 1) 4)) (ifact-helper 1 2 4) (if (> 2 4) 1 (ifact-helper (* 1 2) (+ 2 1) 4)) (ifact-helper 2 3 4) (if (> 3 4) 2 (ifact-helper (* 2 3) (+ 3 1) 4)) (ifact-helper 6 4 4) (if (> 4 4) 6 (ifact-helper (* 6 4) (+ 4 1) 4)) (ifact-helper 24 5 4) (if (> 5 4) 24 (ifact-helper (* 24 5) (+ 5 1) 4)) 24

9 מבוא מורחב 9 Examples of orders of growth FACT Space  (n) – linear Time  (n) – linear IFACT Space  (1) – constant Time  (n) – linear

10 מבוא מורחב 10 The conditional form (cond ( ) ( ) …. ( )) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x))))

11 מבוא מורחב 11 The else clause (cond ( ) ( ) …. ( )) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) (else (- x))))

12 מבוא מורחב 12 Towers of Hanoi Three posts, and a set of different size disks any stack must be sorted in decreasing order from bottom to top the goal is to move the disks one at a time, while preserving these conditions, until the entire stack has moved from one post to another

13 מבוא מורחב 13 Towers of Hanoi (problem definition) Start out with all disks on one peg from largest to smallest Find a sequence of instructions (compliant with the rules) to move all the disks from one peg to another using the third

14 מבוא מורחב 14 Use our paradigm Wishful thinking: Smaller problem: A problem with one disk less How do we use it ? Move n-1 disks from peg A to peg B Move the largest from peg A to peg C Move n-1 disks from peg B to peg C We solve 2 smaller problems !

15 מבוא מורחב 15 Towers of Hanoi (define move-tower (lambda (size from to extra) (cond (move-tower (- size 1) from extra to) (print-move from to) (move-tower (- size 1) extra to from))))) (define print-move (lambda (from to) (write-line ``Move top disk from ``) (write-line from) (write-line `` to ``) (write-line to))) ((= size 1) (print-move from to)) (else

16 מבוא מורחב 16 Towers of Hanoi -- trace (move-tower 3 2 1 3) Move top disk from 2 to 1 Move top disk from 2 to 3 Move top disk from 1 to 3 Move top disk from 2 to 1 Move top disk from 3 to 2 Move top disk from 3 to 1 Move top disk from 2 to 1 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3

17 מבוא מורחב 17 A tree recursion Move 4 Move 3 Move 2 Move 1

18 מבוא מורחב 18 Orders of growth for towers of Hanoi Let T(n) be the number of steps that we need to take to solve the case for n disks. Then T(n) = 2T(n-1) + 1 = 2(2T(n-2) +1) + 1 = 2 n -1 T(1) = 1 So in time we have  (2 n ) -- exponential space --  (n) -- linear We have at most two pending subproblem of any size at any given time.

19 מבוא מורחב 19 Another kind of process Let’s compute a b just using multiplication and addition If b is even, then a b = (a 2 ) (b/2) If b is odd, then a b = a* a (b-1) Note that here, we reduce the problem in half in one step (define fast-exp-1 (lambda (a b) (cond ((= b 1) a) ((even? b) (fast-exp-1 (* a a) (/ b 2))) (else (* a (fast-exp-1 a (- b 1)))))))

20 מבוא מורחב 20 Orders of growth If b even, then 1 step reduces to b/2 sized problem If b odd, 2 steps reduces to b/2 sized problem Thus in 2k steps reduces to b/2 k sized problem We are done when the problem size is just 1, which implies order of growth in time of  (log b) -- logarithmic Space is similarly  (log b) -- logarithmic

21 מבוא מורחב 21 Procedures and their processes Have seen that procedures have different patterns that give rise to different kinds of process evolution Constant Linear Exponential Logarithmic Goal is to begin to learn to recognize these patterns and how to use them in capturing your own procedures


Download ppt "1 Binding names קשירת שמות A occurrence of a name z is bound by the innermost procedure that contains the name and either 1. z is a formal parameter of."

Similar presentations


Ads by Google