Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
6.001 SICP – September Introduction
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
1 Gentle Introduction to Programming Session 4: Arrays, Sorting, Efficiency.
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
מבוא מורחב 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.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
מבוא מורחב 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.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
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 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
Algorithm Analysis Part of slides are borrowed from UST.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
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.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
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.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
1 Programming for Engineers in Python Autumn Lecture 8: Recursion.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
Lecture #5 מבוא מורחב.
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,
High order procedures Primality testing The RSA cryptosystem
Abdulmotaleb El Saddik University of Ottawa
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Higher-Order Programming: Iterative computation (CTM Section 3
Material in the textbook on pages
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CS21b: Structure and Interpretation
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
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.
Lecture #5 מבוא מורחב.
Binding names קשירת שמות
This Lecture Substitution model
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.
Material in the textbook on
Lecture 13 - Assignment and the environments model Chapter 3
Material in the textbook Sections to 1.2.1
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
Lecture 2 מבוא מורחב.
This Lecture Substitution model
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Good programming practices
Presentation transcript:

Principles Of Programming Languages Lecture 2

Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures

Design By Contract Signature: Name of procedure and arguments Purpose: Short text explaining what the procedure does Type Examples of usage Tests indicating correctness Preconditions: Impose a certain obligation to be guaranteed on entry by any client module that calls it Postcondition: Guarantee a certain property on exit

Example Signature: area-of-square(length) Purpose: to compute the area of a rectangle whose side length is ‘length’ Type: [Number -> Number] Example: (area-of-square 5) should produce 25 Pre-conditions: length>= 0 Post-condition: result = length^2 Tests: (area-of-square 2) expected value: 4 (area-of-square -2) expected value: ??? [What do you think?]

Design-By-Contract in this course You are required to write this for every procedure that you write in an exercise In the example shown in class we will often omit it

6 Recursive Procedures How to create a process of unbounded length? Needed to solve more complicated problems. No “for” or “while” loop constructs in scheme! Answer: Recursion!!! Start with a simple example.

מבוא מורחב - שיעור 27 Example: Sum of squares S(n) = ………. …… (n-1) 2 + n 2 Notice that: S(n) = S(n-1) + n 2 S(0) = 0 These two properties completely define the function S(n-1) Wishful thinking: if I could only solve the smaller instance …

מבוא מורחב - שיעור 28 An algorithm for computing sum of squares (define sum-squares (lambda (n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n))))

מבוא מורחב - שיעור 29 Evaluating (sum-squares 3) (define (sum-squares n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n)))) (sum-squares 3) (if (= 3 0) 0 (+ (sum-squares (- 3 1)) (square 3))) (+ (sum-squares (- 3 1)) (square 3)) (+ (sum-squares (- 3 1)) (* 3 3)) (+ (sum-squares (- 3 1)) 9) (+ (sum-squares 2) 9) (+ (if (= 2 0) 0 (+ (sum-squares (- 2 1)) (square 2))) 9) … (+ (+ (sum-squares 1) 4) 9) … (+ (+ (+ (sum-squares 0) 1) 4) 9) (+ (+ (+ (if (= 0 0) 0 (+ (sum-squares (- 0 1)) (square 0))) 1) 4) 9) (+ (+ (+ 0 1) 4) 9) … 14 What would have happened if ‘ if ’ was a function ?

מבוא מורחב - שיעור 210 (sum-squares 3) (if (= 3 0) 0 (+ (sum-squares (- 3 1)) (square 3))) (if #f 0 (+ (sum-squares 2) 9)) (if #f 0 (+ (if #f 0 (+ (sum-squares 1) 4)) 9)) (if #f 0 (+ (if #f 0 (+ (if #f 0 (+ (sum-squares 0) 1)) 4)) 9)) (if #f 0 (+ (if #f 0 (+ (if #f 0 (+ (if #t 0 (+ (sum-squares -1) 0)) 1)) 4)) 9)).. Evaluating (sum-squares 3) with IF as regular form (define (sum-squares n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n)))) We evaluate all operands. We always call (sum-squares) again. We get an infinite loop…….. OOPS

11 General form of recursive algorithms test, base case, recursive case (define sum-sq (lambda (n) (if (= n 0) ; test for base case 0 ; base case (+ (sum-sq (- n 1)) (square n)) ; recursive case ))) base case: small (non-decomposable) problem recursive case: larger (decomposable) problem at least one base case, and at least one recursive case.

מבוא מורחב - שיעור 212 Another example of a recursive algorithm even? (define even? (lambda (n) (not (even? (- n 1))) ; recursive case ))) (if (= n 0) ; test for base case #t ; base case

מבוא מורחב - שיעור 213 Short summary Design a recursive algorithm by 1. Solving big instances using the solution to smaller instances. 2. Solving directly the base cases. Recursive algorithms have 1. test 2. recursive case(s) 3. base case(s)

Iteration Vs. Recursion In scheme there is no “while” or “for” construct Requires getting use to “wishful thinking” – Sometimes more natural! Problem: Recursive processes are “costly’ memory-wise – Need to “remember” pending operations Enter Iterative processes Lots of examples to follow

מבוא מורחב 15 Compute a b (Recursive Approach) wishful thinking : base case: a b = a * a (b-1) a 0 = 1 (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))))

מבוא מורחב 16 Compute a b (Iterative Approach) Another approach: Operationally: Halting condition: product  product * a counter  counter - 1 counter = 0 a b = a 2 *a*…*a= a 3 *…*a Which is: a b = a * a * a*…*a b

17 Compute a b (Iterative Approach) (define (exp-2 a b) (define (exp-iter a counter product) (if (= counter 0) product (exp-iter a (- counter 1) (* a product))) (exp-iter a b 1)) Syntactic Recursion How then, do the two procedures differ? They give rise to different processes – lets use our model to understand how.

מבוא מורחב 18 Recursive Process (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1)))))) (exp-1 3 4) (* 3 (exp-1 3 3)) (* 3 (* 3 (exp-1 3 2))) (* 3 (* 3 (* 3 (exp-1 3 1)))) (* 3 (* 3 (* 3 (* 3 (exp-1 3 0))))) (* 3 (* 3 (* 3 (* 3 1)))) (* 3 (* 3 (* 3 3))) (* 3 (* 3 9)) (* 3 27) 81

מבוא מורחב 19 Iterative Process (exp-2 3 4) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter ) (exp-iter ) 81 (define (exp-2 a b) (define (exp-iter a counter product) (if (= counter 0) product (exp-iter a (- counter 1) (* a product))) (exp-iter a b 1))

מבוא מורחב 20 The Difference (exp-2 3 4) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter ) (exp-iter ) 81 (exp-1 3 4) (* 3 (exp-1 3 3)) (* 3 (* 3 (exp-1 3 2))) (* 3 (* 3 (* 3 (exp-1 3 1)))) (* 3 (* 3 (* 3 (* 3 (exp-1 3 0))))) (* 3 (* 3 (* 3 (* 3 1)))) (* 3 (* 3 (* 3 3))) (* 3 (* 3 9)) (* 3 27) 81 Growing amount of space Constant amount of space

21 Why More Space? Recursive exponentiation: (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))) operation pending Iterative exponentiation: (define (exp-2 a b) (define (exp-iter a counter product) (if (= counter 0) product (exp-iter a (- counter 1) (* a product)))) (exp-iter a b 1)) no pending operations

22 Example: Factorial wishful thinking : base case: n! = n * (n-1)! n = 1 (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1)))))) Iterative or Recursive?

23 Computing SQRT: A Numeric Algorithm To find an approximation of square root of x, use the following recipe: Make a guess G Improve the guess by averaging G and x/G Keep improving the guess until it is good enough G = 1X = 2 X/G = 2G = ½ (1+ 2) = 1.5 X/G = 4/3G = ½ (3/2 + 4/3) = 17/12 = X/G = 24/17G = ½ (17/ /17) = 577/408 =

24 (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define (good-enough? guess x) (< (abs (- (square guess) x)) precision)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt x) (sqrt-iter initial-guess x)) (define initial-guess 1.0) (define precision )

מבוא מורחב - שיעור 425 Towers of Hanoi Three posts, and a set of disks of different sizes. A disk can be placed only on a larger disk (or on bottom). At the beginning all the disks are on the left post. The goal is to move the disks one at a time, while preserving these conditions, until the entire stack has moved from the left post to another You are allowed to move only the topmost disk at a step

מבוא מורחב - שיעור 426 Use our paradigm Wishful thinking: Smaller problem: A problem with one disk less How do we use it ? To move n disks from post A to post C (using B as aux): Move top n-1 disks from post A to post B (using C as aux) Move the largest disk from post A to post C Move n-1 disks from post B to post C (using A as aux) We solve 2 smaller problems !

מבוא מורחב - שיעור 427 Towers of Hanoi (define (one-move from to) (display "Move top disk from ") (display from) (display " To ") (display to) (newline)) (define (move-tower size from to aux) (cond ((= size 1) (one-move from to)) (else (move-tower (- size 1) from aux to) (one-move from to) (move-tower (- size 1) aux to from))))

מבוא מורחב - שיעור 428 Tree Recursion (mt ) (mt ) (mt ) (one-move 1 2) (mt ) (one-move 3 2) (mt ) (one-move 1 3) (mt ) (one-move 2 3)(one-move 3 1)(one-move 1 2)

מבוא מורחב - שיעור 429 Towers of Hanoi -- trace (move-tower ) Move top disk from 1 to 2 Move top disk from 1 to 3 Move top disk from 2 to 3 Move top disk from 1 to 2 Move top disk from 3 to 1 Move top disk from 3 to 2 Move top disk from 1 to a3a (move-tower ) (move-tower )

30 Time Complexity for towers of Hanoi Denote by T(n) the number of steps that we need to take to solve the case for n disks. T(n) = 2T(n-1) + 1 T(1) = 1 This solves to: T(n) = 2 n -1=  (2 n ) What does that mean ?

31 Hanoi Towers Say we want to solve the problem for 400 disks. Say it takes a second to move a disk. We need about seconds. That’s about years. That’s about millenniums. Might be longer then the age of the universe …. Infeasible !!!!

32 Let ’ s buy a fast computer and make it feasible. Our new computer can move giga billion (2 60 ) disks a second. Absolutely the last word in the field of computing. We need about seconds. That’s about years. That’s about millenniums. Does not help much. Infeasible !!!!