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

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP – September Introduction
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
מבוא מורחב 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.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Lecture 14: Assignment and the Environment Model (EM)
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti
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.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
3/8/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
Higher-Order Programming: Iterative computation (CTM Section 3
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Edited by Original material by Eric Grimson
Computing Square Roots
Higher-Order Programming: Iterative computation (CTM Section 3
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CS21b: Structure and Interpretation
Algorithm design and Analysis
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.
Functional Programming
Binding names קשירת שמות
This Lecture Substitution model
CS220 Programming Principles
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Material in the textbook Sections to 1.2.1
Lecture 2 מבוא מורחב.
This Lecture Substitution model
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Good programming practices
Presentation transcript:

CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 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 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)) ( ) 136

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

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

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 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 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 Exercise 1.6 n See the textbook. Check the program. SquareRoot.scm

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

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 Imperative Style s(n) = … +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 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 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 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 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 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 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 Procedure and Process n Recursive procedure (define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))) (fact 5)

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 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 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 ) (fact-iter ) 120

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 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 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 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 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 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 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 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 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 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 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 Trace of Exps n (exp1 2 4) n (exp2 2 4)

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 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 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 Excecise Recursive order (define (foo n) (if (< n 3) n (+ (foo (- n 1)) (* 2 (foo (- n 2))) (* 3 (foo (- n 3))))))

39 Excecise 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 n))

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