Binding names A occurrence of a name z
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((( 1
עידון מודל ההצבה When you apply F replace only occurences bound by F of each formal parameter. Do not replace occurences bound by an internal definition! מבוא מורחב 2
)define (h ) define ) g (* 2 ) <=h x( (f y) (+ x y(( (g x) (f x((( 1( )define (f y) )define (g x) )g (* 2 1(( (+ 1 y(( (f x(( )g 2( )f 2( (2 1 +) 3 מבוא מורחב 3
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) Q(f(n)) if R(n) O(f(n)) and R(n) (f(n)) 100 n 3 + 10 n O(n 3) 100 n 3 + 10 n Q (n 3) *10 log(n) + loglog(n) Q ( log(n)) מבוא מורחב 5
Examples of orders of growth • FACT • Space S(n) Q(n) – linear • Time T(n) Q(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. מבוא מורחב 7
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 (ifact-helper 1 2 4) (if (> 2 4) 1 (ifact-helper (* 1 2) (+ 2 (ifact-helper 2 3 4) (if (> 3 4) 2 (ifact-helper (* 2 3) (+ 3 (ifact-helper 6 4 4) (if (> 4 4) 6 (ifact-helper (* 6 4) (+ 4 (ifact-helper 24 5 4) (if (> 5 4) 24 (ifact-helper (* 24 5) (+ 24 1) 4)) 5 1) 4)) 8
Examples of orders of growth • FACT • Space Q(n) – linear • Time Q(n) – linear • IFACT • Space Q(1) – constant • Time Q(n) – linear מבוא מורחב 9
The conditional form )cond (<test-1> <consequent-1(< >) test-2> <consequent-2(< . … >) test-n> <consequent-n((< )define (abs x( )cond ((> x 0) x( =)) x 0) 0( >)) x 0) (- x(((( מבוא מורחב 10
The else clause )cond (<test-1> <consequent-1(< >) test-2> <consequent-2(< . … >) else> <consequent-n>)) )define (abs x( )cond ((> x 0) x( =)) x 0) 0( ) else (- x)))) מבוא מורחב 11
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 מבוא מורחב 12
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 מבוא מורחב 13
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 ! מבוא מורחב 14
Towers of Hanoi )define move-tower (lambda (size from to extra) (cond =))size 1) (print-move from to)) (else (move-tower (- size 1) from extra to) (print-move from to) (move-tower (- size 1) extra to from))))) )define print-move (lambda (from (write-line to) ``Move top disk from ``) from) `` to ``) to))) מבוא מורחב 15
Towers of Hanoi -- trace )move-tower 3 Move top disk Move top disk 2 1 3) from 2 from 1 from 2 from 3 from 2 to to 1 3 3 1 2 1 1 222222 מבוא מורחב 111111 333333 16
A tree recursion Move 4 Move 3 Move 2 Move 1 Move 1 מבוא מורחב Move 2 Move 1 17
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) = 2 T(n-1) + 1 = 2(2 T(n-2) +1) + 1 = 2 n -1 T(1) = 1 • So in time we have Q(2 n) -- exponential • space -- Q(n) -- linear We have at most two pending subproblem of any size at any given time. מבוא מורחב 18
Another kind of process • • Let’s compute ab just using multiplication and addition If b is even, then ab = (a 2)(b/2) If b is odd, then ab = 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))))))) מבוא מורחב 19
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 2 k 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 Q(log b) -logarithmic • Space is similarly Q(log b) -- logarithmic מבוא מורחב 20
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 מבוא מורחב 21
- Slides: 21