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

44
CS220 Programming Principles 프프프프프프 프프 2003 프프프프 Class 2 프프

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

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

CS220Programming Principles

프로그래밍의 이해2003 가을학기 Class 2

한 태숙

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

2

Substitution Model

To evaluate a combination– Evaluate the subexpressions of the combination– Apply the procedure to arguments

Apply procedure to arguments– Evaluate the body of the procedure with each

formal parameter replaced by corresponding argument

called applicative-order evaluation

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

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

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

4

Normal-Order Evaluation

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

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

5

Exercise 1.5

To test the interpreter?

(define (p) (p))

(define (test x y) (if (= x 0) 0 y))

(test 0 (p))

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

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

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

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))

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

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)

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

9

Exercise 1.6

See the textbook. Check the program.

SquareRoot.scm

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

10

Examples(IV)

(odd? n)(define (odd? n)

(if (zero? n)

#f

(not (odd? (dec n))))) (odd? 3)

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

11

Exercise

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))))

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

12

Imperative Style

s(n) = 02 + 12 + … +n2

--> starting with i=0 repeatly add i2 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:

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

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))

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

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 )))

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

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;

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

16

Black Box Abstraction

Procedural decomposition of programs

(define x 3)(define (square x) (* x x))(define (good-enough? guess ) (< (abs (- (square guess) x)) 0.001))

Bound variable vs. Free variable Scope of variable

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

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))

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

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))

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

19

Procedure and Process

Recursive procedure(define (fact n)

(if (= n 1)

1

(* n (fact (- n 1)))))

(fact 5)

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

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

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

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))) recursive procedure?

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

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

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

23

Tree recursion

Fibonacci number– 0, 1, 1, 2, 3, 5, 8, 13, 21, …

Fib(n) = 0 if n = 0 Fin(n) = 1 if n = 1 Fin(n) = Fib(n-1) + Fib(n-2) otherwise

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

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)->

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

25

Operation count for Combination

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 <operator> and <arg>’s, and– the number of ops for the application of the

value of <operator> to the value of the <arg>’s.

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

26

Operation count for application

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) <body>)

is the number of ops of <body> in the environment with x given the value M, and y given the value N.

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

27

How many

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...

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

28

Exponential growth!?

#ops(n) > 2 #ops(n-2)

> 2 (2 #ops(n-4))

> 2n/2

=(1.4)n

Fib(n) = n/51/2

where is golden ratio(

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

29

Fast Fibonacci (Iterative version)

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

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

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))))

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

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

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

32

Exponentiation - First try

b0 = 1; b n = b * b n-1

(define exp1

(lambda (b n)

(if (= n 0)

1

(* b (exp1 b (- n 1)))))

Linear recursive process

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

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)))

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

34

Trace of Exps

(exp1 2 4)

(exp2 2 4)

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

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 k1 and k2 independent of n and an n0 such that for all n > n0 :

k1 f(n) <= R(n) <= k2 f(n) We say that R(N) has order of growth O(f(n)) if there

is some constant K and an n0 such that for all n > n0 :

R(n) <= K f(n)

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

36

Fast Exponentiation

b0 = 1; b2n = (bn)2; b2n+1 = b * b2n

(define (fast-exp b n) (if (zero? n) 1 (if (even? n) (square (fast-exp b (/ n 2))) (* b (fast-exp b (- n 1))))))

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

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*log2n + 1

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

38

Excecise 1.11 - Recursive order

(define (foo n)

(if (< n 3)

n

(+ (foo (- n 1))

(* 2 (foo (- n 2)))

(* 3 (foo (- n 3))))))

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

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))

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

40

Variable Scope

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))

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

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) <body>)

<Sch-expr>)

is the values of <Sch-expr> in the environment with x given the value n, and with a proc-helper procedure defined by the internal definition.

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

42

Scheme’s Scoping Rules

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.

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.

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

43

Tail Recursive Procedures

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

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

44

Tail Recursive Procedure(more)

Very restricted procedure definition format. Have a simple step-by-step computational

model: move a “finger” along the text of definition.

Computational Space for a procedure application is bounded by the size of the text of the definition: Space used is O(1).