Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.

Similar presentations


Presentation on theme: "CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙."— Presentation transcript:

1 CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙

2 2 Substitution Model n To evaluate a combination –Evaluate the subexpressions of the combination –Apply the procedure to arguments n Apply procedure to arguments –Evaluate the body of the procedure with each formal parameter replaced by corresponding argument n called applicative-order evaluation

3 3 Example of Substitution Model (define (f a) (sum-of-square (+ a 1) (* a 2))) (f 5) (sum-of-squares (+ 5 1) (* 5 2)) (+ (square 6) (square 10)) (+ (* 6 6) (* 10 10)) (+ 36 100) 136

4 4 Normal-Order Evaluation n Fully expand and reduce (f 5) (sum-of-square (+ 5 1) (* 5 2)) (+ (square (+ 5 1))(square (* 5 2))) (+(*(+ 5 1)(+ 5 1))(*(* 5 2)(* 5 2)) reduce…….. (+ (* 6 6) (* 10 10)) (+ 36 100) 136

5 5 Exercise 1.5 To test the interpreter? (define (p) (p)) (define (test x y) (if (= x 0) 0 y)) (test 0 (p))

6 6 Square Root Program (review) Definition of Square Root (define (sqrt x) (the y (and (>= y 0) (= (square y) x)))) Newton’s Method new approximation = (old approximation + x/old approximation)/2

7 7 Example : Square Root(I) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define (improve guess x) (average guess (/ x guess))) (define (average x y) (/ (+ x y) 2))

8 8 Example : Square Root(II) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (sqrt x) (sqrt-iter 1.0 x)) (sqrt 9) 

9 9 Exercise 1.6 n See the textbook. Check the program. SquareRoot.scm

10 10 Examples(IV) n (odd? n) (define (odd? n) (if (zero? n) #f (not (odd? (dec n))))) n (odd? 3)

11 11 Exercise n odd?(n) and even?(n) (define (odd? n) (if (= n 0) #f (even? (- n 1)))) (define (even? n) (if (= n 0) #t (odd? (- n 1))))

12 12 Imperative Style s(n) = 0 2 + 1 2 + … +n 2 --> starting with i=0 repeatly add i 2 to the running sum and increment i until i exceeds n start: i:=0; s:=0; loop: if i = n+1 goto end; temp := square i; i := i+1; s := s+temp; goto loop; end:

13 13 Imperative version (define (sum-sq-imp n) (loop 0 0 n)) (define (loop i s n) (if (= i (inc n)) (end s) (next i s n (square i)))) (define (next i s n temp) (loop (inc i ) (+ s temp) n))

14 14 Imperative version (define (sum-sq-imp n) (loop 0 0 n)) (define (loop i s n) (if (= i (inc n)) (end s) (loop (inc i ) (+ s (square i)) n )))

15 15 Imperative Odd? Even? start: if (n = 0) goto end-false; n := n -1; if (n= 0) goto end-true; n := n-1; goto start;

16 16 Black Box Abstraction n Procedural decomposition of programs (define x 3) (define (square x) (* x x)) (define (good-enough? guess ) (< (abs (- (square guess) x)) 0.001)) n Bound variable vs. Free variable n Scope of variable

17 17 Block Structure (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess x)).001)) (define (improve guess x) (average guess (/ x guess))) (define (try guess x) (if (good-enough? guess x) guess (try (improve guess x) x))) (try 1 x))

18 18 Lexical Scoping (define (sqrt x) (define (good-enough? guess) (< (abs (- (square (guess x)).001)) (define (improve guess) (average guess (/ x guess))) (define (try guess) (if (good-enough? guess) guess (try (improve guess)))) (try 1))

19 19 Procedure and Process n Recursive procedure (define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))) (fact 5)

20 20 Linear recursive process (fact 5) (* 5 (fact 4)) (* 5(* 4 (fact 3)) (* 5(* 4 (* 3 (fact 2))) (* 5(* 4 (* 3 (* 2 (fact 1)))) (* 5(* 4 (* 3 (* 2 1)))) (* 5(* 4 (* 3 2))) (* 5(* 4 6))) (* 5 24) 120

21 21 Iterative version of factorial (define (fact n) (fact-iter 1 1 n)) (define (fact-iter prod cnt max-n) (if (> cnt max-n) prod (fact-iter (* cnt prod) (+ cnt 1) max-n))) n recursive procedure?

22 22 Linear iterative process (fact 5) (fact-iter 1 1 5) (fact-iter 1 2 5) (fact-iter 2 3 5) (fact-iter 6 4 5) (fact-iter 24 5 5) (fact-iter 120 6 5) 120

23 23 Tree recursion n Fibonacci number –0, 1, 1, 2, 3, 5, 8, 13, 21, … n Fib(n) = 0 if n = 0 n Fin(n) = 1 if n = 1 n Fin(n) = Fib(n-1) + Fib(n-2) otherwise

24 24 Recursive Fibonacci Procedure (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (fib 4)->(fib 3)->(fib 2)->(fib 1) ->(fib 0)->(fib 1)->(fib 2)-> (fib 1)->(fib0)->

25 25 Operation count for Combination n The number of operations needed to find the value of a combination is the sum of –the number of operations needed to find the values of and ’s, and –the number of ops for the application of the value of to the value of the ’s.

26 26 Operation count for application n The number of ops for the application of a compound procedure named proc applied to values M, N in an environment where proc is define by (define (proc x y) ) is the number of ops of in the environment with x given the value M, and y given the value N.

27 27 How many n primitive operations to evaluate Fib(n)? #ops(0) = #ops(1) = 1 ; just “<“ #ops(n) = #ops(n-1)+#ops(n-2)+4 ( one “<“, one “+”, and two “-”) But clearly #ops(n-1)>=#ops(n-2) So...

28 28 Exponential growth!? #ops(n) > 2 #ops(n-2) > 2 (2 #ops(n-4)) > 2 n/2 =(1.4) n Fib(n) =  n /5 1/2 where  is golden ratio(   

29 29 Fast Fibonacci (Iterative version) n Faster Fib is based on the idea of keeping the value of Fib(k-1) when doing F(k). Let a be Fib(k-1) and b Fib(k-2) then keep updating a := a+b b := a simultaneously

30 30 Iterative Fibonacci - Linear time (define (fast-fib n) (fib-iter 1 0 n)) (define (fib-iter a b n) (if (zero? n) b (fib-iter (+ a b) a (- n 1))))

31 31 Time and Space Complexity (fast-fib 5) (fib-iter 1 0 5) … … Time Complexity: #ops(n) = 3 + #ops(n-1) = 3n+1 Linear Growth! Space Complexity: Constant

32 32 Exponentiation - First try n b 0 = 1; b n = b * b n-1 (define exp1 (lambda (b n) (if (= n 0) 1 (* b (exp1 b (- n 1))))) Linear recursive process

33 33 Exponentiation - Second try (define exp2 ;linear iterative (lambda (b n) (define exp-iter (lambda (b count prod) (if (= count 0) prod (exp-iter b(- count 1) (* prod b)))) (exp-iter b n 1)))

34 34 Trace of Exps n (exp1 2 4) n (exp2 2 4)

35 35 Orders of Growth We say that function R of parameter n has order of growth  (f(n)), written R(n) =  (f(n)) if there are positive constants k 1 and k 2 independent of n and an n 0 such that for all n > n 0 : k 1 f(n) <= R(n) <= k 2 f(n) n We say that R(N) has order of growth O(f(n)) if there is some constant K and an n 0 such that for all n > n 0 : R(n) <= K f(n)

36 36 Fast Exponentiation n b 0 = 1; b 2n = (b n ) 2 ; b 2n+1 = b * b 2n (define (fast-exp b n) (if (zero? n) 1 (if (even? n) (square (fast-exp b (/ n 2))) (* b (fast-exp b (- n 1))))))

37 37 Fast-exp operations -Log growth If n is even #ops(n) = 4 + #ops(n/2) If n is odd #ops(n) = 4+#ops(n-1) = 4+(4+#ops((n-1)/2)) So,#ops(n)<= 8+#ops(floor(n/2)) <= 8+(8+#ops(floor(n/4))) <= 8*log 2 n + 1

38 38 Excecise 1.11 - Recursive order (define (foo n) (if (< n 3) n (+ (foo (- n 1)) (* 2 (foo (- n 2))) (* 3 (foo (- n 3))))))

39 39 Excecise 1.11 - Iterative order (define (foo-i n) (define (foo-iter a b c count) (if (= count 0) c (foo-iter (+ a (* 2 b)(* 3 c)) a b (- count 1)))) (foo-iter 2 1 0 n))

40 40 Variable Scope n dropping parameter b, and hide h outside (define (fast-exp b n) (define (h m) (if (zero? m) 1 (if (even? m) (square (h (/ m 2))) (* b (h (- m 1)))))) (h n))

41 41 Internal Defines: The value of a compound procedure name proc applied to a value, n, in an environment where proc is defined by (define (proc x) (define (proc-helper y) ) ) is the values of in the environment with x given the value n, and with a proc- helper procedure defined by the internal definition.

42 42 Scheme’s Scoping Rules n Procedure parameters and internal definitions are invisible outside the procedure. Redefining them outside the procedure will not affect the procedure’s behavior in any way. n Renaming an internal name into any “fresh” name - a name which is no already an internal name - will not affect the computational behavior of anything at all.

43 43 Tail Recursive Procedures n Easy to simulate step-by-step, remembering only two things: –a single position in the code being evaluated, and –the values of the variables mentioned in the code example: iterative versions of sample programs

44 44 Tail Recursive Procedure(more) n Very restricted procedure definition format. n Have a simple step-by-step computational model: move a “finger” along the text of definition. n Computational Space for a procedure application is bounded by the size of the text of the definition: Space used is O(1).


Download ppt "CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙."

Similar presentations


Ads by Google