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

Slides:



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

CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
מבוא מורחב - שיעור 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).
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
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 Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
PPL Pairs, lists and data abstraction. Compound Data Until now: atomic, unrelated entities Now: organized into structures Why? – Better conceptual level.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
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.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Data abstractions
The Metacircular Evaluator
The Metacircular Evaluator
Material in the textbook on
Abstraction and Repetition
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Lecture 2 מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Abstraction and Repetition
Presentation transcript:

Principles Of Programming Languages Lecture 2

Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live demo!!!

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

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

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 ; Pre-conditions: x >= 0

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

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

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

Union Types What is the type of: (define foo (lambda (x) (if (> x 10) x #t)))

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

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

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

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?

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

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)

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)

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 ]

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

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

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 )

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

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

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

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

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

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

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

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

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 )) (list-ref squares 4)

Operator: length (define length (λ (l) (if (null? l) 0 (+ 1 (length (cdr l)))))) (define squares (list )) (length squares) 6

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

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

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:

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

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

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.

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

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.

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

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.

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)

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

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

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

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.

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

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

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

52 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

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

Frame Space in RAM needed for procedure call evaluation.

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.

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

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

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

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

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 !

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

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

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

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 ?

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 seconds. That’s about years. That’s about millenniums. Might be longer then the age of the universe …. Infeasible !!!!

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 seconds. That’s about years. That’s about millenniums. Does not help much. Infeasible !!!!