Presentation is loading. Please wait.

Presentation is loading. Please wait.

Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!

Similar presentations


Presentation on theme: "Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!"— Presentation transcript:

1

2 Principles Of Programming Languages Lecture 2

3 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!

4 Design by Contract Approach for software design Metaphor of business contract between client and supplier: – Obligations – Benefits Introduced by Meyer [88]

5 Design By Contract 1.Signature: Name of procedure and arguments 2.Purpose: Short text explaining what the procedure does 3.Type 4.Examples of usage 5.Pre-conditions: Impose a certain obligation to be guaranteed on entry by any client module that calls it 6.Post-condition: Guarantee a certain property on exit 7.Tests indicating correctness 8.Invariants rules of correctness

6 Example of a Contract 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

7 Design by Contract Mandatory: – Signature – Purpose – Type – Tests (only for complex procedures – usually the case) Examples are desirable Conditions and invariants only if non-trivial

8 Design by Contract SupplierClient Post-conditionPre-conditionObligation Pre-conditionPost-conditionBenefit

9 Design-By-Contract in this course All procedures in assignments must have contracts! Mandatory: signature, purpose, type and tests Examples are recommended Pre-condition can be written in plain English Post-condition is recommended

10

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

12 11 Example: Sum of squares S(n) = 0 2 + 1 2 + 2 2 ………. …… (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 …

13 12 An algorithm for computing sum of squares (define sum-squares (lambda (n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n))))) If you don’t remember the code of this procedure – look at yesterday’s slides.

14 13 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 ?

15 14 (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 (lambda (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

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

17 16 Another example of a recursive algorithm even? (define even? (lambda (n) (not (even? (- n 1))) ; recursive case ))) ; not is a primitive procedure that negates a boolean (if (= n 0) ; test for base case #t ; base case

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

19 Iteration Vs. Recursion Problem: Recursive processes are “costly’ memory-wise – Need to “remember” pending operations Enter Iterative processes Lots of examples to follow

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

21 20 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

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

23 Compute a b Recursive (define exp-1 (λ (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1)))))) Iterative (define exp-iter (λ (a counter product) (if (= counter 0) product (exp-iter a (- counter 1) (* a product))))

24 23 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

25 24 Iterative Process (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter 3 1 27) (exp-iter 3 0 81) 81 (define exp-iter (lambda (a counter product) (if (= counter 0) product (exp-iter a (- counter 1) (* a product)))))

26 25 The Difference (exp-2 3 4) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter 3 1 27) (exp-iter 3 0 81) 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

27 26 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-iter a counter product) (if (= counter 0) product (exp-iter a (- counter 1) (* a product))))) no pending operations

28 Frame Space in RAM needed for procedure call evaluation.

29 Tail Position A property of a procedure: the recursive call is the last evaluation step. Compilers can identify it automatically and not open another frame – thus making it iterative.

30 29 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?

31 30 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 = 1.416666 X/G = 24/17G = ½ (17/12 + 24/17) = 577/408 = 1.4142156

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

33 32 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

34 33 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 !

35 Towers of Hanoi (define move-tower (lambda (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))))) (define one-move (lambda (from to) (display "Move top disk from ") (display from) (display " To ") (display to) (newline))) 34

36 35 Tree Recursion (mt 3 1 2 3) (mt 2 1 3 2) (mt 1 3 1 2) (one-move 1 2) (mt 2 3 2 1) (one-move 3 2) (mt 1 2 3 1) (one-move 1 3) (mt 1 1 2 3) (one-move 2 3)(one-move 3 1)(one-move 1 2)

37 36 Towers of Hanoi -- trace (move-tower 3 1 2 3) 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 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3a3a (move-tower 2 1 3 2) (move-tower 2 3 2 1)

38 37 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 ?

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

40 39 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 2 340 seconds. That’s about 2 313 years. That’s about 2 303 millenniums. Does not help much. Infeasible !!!!


Download ppt "Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!"

Similar presentations


Ads by Google