Presentation is loading. Please wait.

Presentation is loading. Please wait.

Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.

Similar presentations


Presentation on theme: "Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live."— Presentation transcript:

1

2 Principles Of Programming Languages Lecture 2

3 Today Design-By-Contract Data Structures: Lists and Pairs 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 of input parameters and return value 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: sqrt(x) ; Purpose: To compute the square root of x, using Newton’s approximations method ; Type: [Number -> Number] ; Example: (sqrt 16.) should produce 4.000000636692939 ; Pre-conditions: x >= 0

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 Union Types What is the type of: (define foo (lambda (x) (if (> x 10) x #t)))

11 Union Type ;… ; Type [Num -> Num union Boolean] ; … (define foo (lambda (x) (if (> x 10) x #t)))

12 Compound Data Until now: atomic, unrelated entities Now: organized into structures Why? – Better conceptual level of design – Modularity – Maintenance – Code reuse Actually a new type in the signature

13 Data Abstractions in Scheme? Of course, but first we need to present 2 new data types in Scheme: – Pairs – Lists

14 Pairs Combines two data entities into a single unit Scheme provides built in primitives: – Value constructor: cons – Selectors: car, cdr – Identify predicate: pair? – Equality: equal?

15 Pairs > (define x (cons 1 2)) > (car x) 1 > (cdr x) 2 > x (1. 2) “toString” of pair

16 Pairs Each data entity can be anything! > (define y (cons x (quote a))) > (car y) (1. 2) > (cdr y) 'a > y ((1. 2). a) Recall that x is the pair (cons 1 2)

17 Pairs Do not confuse (cons 1 2) with (1. 2) !! 1 st is a Scheme expression (syntax) and 2 nd is the ‘toString’ of the value (same as lambda and procedure)

18 Pair Type Type constructor: Pair Type of: cons : [T 1 *T 2 -> Pair(T 1,T 2 )] car : [Pair(T 1,T 2 ) -> T 1 ] cdr : [Pair(T 1,T 2 ) -> T 2 ]

19 Symbols Type is Symbol, and values are atomic names (NOT strings!) Constructor: quote

20 Symbol Type > (quote a) a > ’a a > (define a ’a) > a a > (define b a) > b a > (eq? a b) #t > (symbol? a) #t > (define c 1) > (symbol? c) #f > (number? c) #t Symbols are atomic types, their values unbreakable: ‘abc is just a symbol Primitive procedure that compares two values Primitive procedure that checks if the value is of type symbol

21 Lists Finite sequence  (v 1 … v n ) v 1 is head, (v 2 … v n ) is tail Value constructors: cons and list Recursive definition: – Empty: empty (list) – Non-empty: (cons head tail) – tail must be a list! (list e 1 … e n )

22 List Implementation Lists are actually nested pairs! The inmost item is an empty list Printing form of list is nice: – ( … )

23 Lists Both will create the same list: (list 1 2 3) (cons 1 (cons 2 (cons 3 (list))))

24 Lists Selectors: – car : head – cdr : tail Predicates: – list? – empty? (null?) – equal?

25 Examples > (define one-through-four (list 1 2 3 4)) > one-through-four (1 2 3 4) > (car one-through-four) 1 > (cdr one-through-four) (2 3 4) > (car (cdr one-through-four)) 2

26 Note on Pairs and Lists Pair and List types have same value constructor and selector Scheme can live with it because its dynamically typed

27 Visual Representation Empty list Non empty list ((1 2) 3 4) : 3124

28 Lists and Types Homogenous – Examples: (1 2 3), ((1 2) (3)) – LIST(Number), LIST(T), … Heterogeneous – Examples: (1 #f 3), ((1 2) 3) – LIST

29 Useful List Operations car + cdr: > (define x (list 5 6 8 2)) > (car x) 5 > (cdr x) (6 8 2) > (car (cdr x)) 6 > (cadr x) 6 > (cddr x) (8 2)

30 Selector: list-ref n th element of a list: ;; Type: [LIST * Number -> T] (define list-ref (λ (l n) (if (= n 0) (car l) (list-ref (cdr l) (- n 1))))) (define squares (list 1 4 9 16 25 36)) (list-ref squares 4)

31 Operator: length (define length (λ (l) (if (null? l) 0 (+ 1 (length (cdr l)))))) (define squares (list 1 4 9 16 25 36)) (length squares) 6

32 Operator: append (define append (λ (l1 l2) (if (null? l1) l2 (cons (car l1) (append (cdr l1) l2))))) (append (list 1 2 3) (list 3 4))

33 Constructor make-list Builds a list of given with given values (define make-list (λ (l v) (if (= l 0) (list) (cons v (make-list (- l 1) v)))))

34 Using Lists to Represent Trees Unlabeled trees: – Empty tree () – Leaf is just value – Non-empty tree – non-empty list Example: (1 (2 3)) is the tree:

35 Using Lists to Represent Trees How can we add data to non-leaf nodes? (i.e. labeled tree) : first element is the root: – (1 (0) (3 (2) (4)))

36 Leaves Count Unlabeled tree (define count-leaves (λ (t) (cond ((null? t) 0) ((not (list? t)) 1) (else (+ (count-leaves (car t)) (count-leaves (cdr t)))))))

37

38

39

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

41 40 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 …

42 41 An algorithm for computing sum of squares (define sum-squares (lambda (n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n))))) We defined square earlier.

43 42 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

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

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

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

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

48 47 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

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

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

51 50 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

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

53 52 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

54 53 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

55 Frame Space in RAM needed for procedure call evaluation.

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

57 Fibonacci: Recursive vs Iterative (define fib (lambda (n) (cond [(= n 0) 0] [(= n 1) 1] [else (+ (fib (- n 1)) (fib (- n 2)))])))

58 (fib 3)(fib 2)(fib 1)(fib 0)(fib 1) (define fib (lambda (n) (cond [(= n 0) 0] [(= n 1) 1] [else (+ (fib (- n 1)) (fib (- n 2)))])))

59 (define fib2 (lambda (n) (fib-iter n 1 0))) (define fib-iter (lambda (n n-1 n-2) (cond [(= n 0) n-2] [(= n 1) n-1] (else (fib-iter (- n 1) (+ n-1 n-2) n-1)))))

60 59 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

61 60 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 !

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

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

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

65 64 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 ?

66 65 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 !!!!

67 66 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 Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live."

Similar presentations


Ads by Google