Plt-2002-2 9/8/2015 2.2-1 Inductive Sets of Data Programming Language Essentials 2nd edition Chapter 1.2 Recursively Specified Programs.

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
SICP Data abstraction revisited Data structures: association list, vector, hash table Table abstract data type No implementation of an ADT is necessarily.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
A Scheme Refresher (Functional Subset) Prabhaker Mateti.
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
1 Lecture 16: Lists and vectors Binary search, Sorting.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
For B551 (and B351) Todd Holloway Indiana University Please download Petite Chez Scheme (Scheme.com) If you haven’t already…
Chapter 1: Inductive Sets of Data Recall definition of list  '() is a list.  If l is a list and a is any object, then (cons a l ) is a list More generally:
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
331 Final Fall Details 3:30-5:30 Friday December 17 th LH4 Comprehensive with more emphasis on material since the midterm Study example finals and.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof:  '(abc 123) = (cons 'abc (cons 123 '()))  '() is a list, so (cons 123.
Class 7: List Procedures David Evans cs1120 Fall 2009.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Recursive Algorithms Section 5.4.
Abstract Syntax cs7100 (Prasad) L7AST.
Chapter 1: Inductive Sets of Data
CS314 – Section 5 Recitation 10
CS 550 Programming Languages Jeremy Johnson
Introduction to Scheme
A I (Artificial Intelligence)
Chapter 2: Data Abstraction 2
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lists in Lisp and Scheme
COP4020 Programming Languages
Env. Model Implementation
Class 19: Think Globally, Mutate Locally CS150: Computer Science
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
Chapter 1 Review: BNF Grammar for lists:
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
topics mutable data structures
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
topics interpreters meta-linguistic abstraction eval and apply
Lisp.
Lecture 8: Recursing Lists CS150: Computer Science
Presentation transcript:

plt /8/ Inductive Sets of Data Programming Language Essentials 2nd edition Chapter 1.2 Recursively Specified Programs

plt /8/ Recursion recursion can lead to divide-and-conquer: solution for a small problem 'by hand'. solution for a bigger problem by recursively invoking the solution for smaller problems. Examples: Factorial. Greatest common divisor (Euclid). Tower of Hanoi. Quicksort.

plt /8/ Recursively Specified Programs patterned after proof by induction. to compute x n : e(0, x) = 1, because x 0 is 1. e(n, x) = x * e(n-1, x), because x n is x * x n-1. induction proves that e(n,x) is x n : (0) Hypothesis: e(k, x) is x k. (1)true for k=0, see above. (2)assume true up to k. Consider e(k+1, x). This is x * e(k, x) by definition, x * x k by assumption, and therefore x k+1.

plt /8/ Recursively Specified Programs (2) to compute x n : e(0, x) = 1, because x 0 is 1. e(n, x) = x * e(n-1, x), because x n is x * x n-1. (define e (lambda (n x) (if (zero? n) 1 (* x (e (- n 1) x) ) )

plt /8/ count-nodes bintree: 'Number' | '(' 'Symbol' bintree bintree ')' (define count-nodes (lambda (bintree) (if (number? bintree) 1 (+ (count-nodes (cadr bintree)) (count-nodes (caddr bintree)) 1 ) )

plt /8/ Deriving Programs from BNF l-of-nums: '()' l-of-nums: '(' 'Number' '.' l-of-nums ')' pattern program after data (structural induction) one procedure for one nonterminal (define list-of-numbers? (lambda (x) (if (null? x) #t (and (pair? x) (number? (car x)) (list-of-numbers? (cdr x)) ) )

plt /8/ Proof by Induction (0) Hypothesis: l-of-n? works on lists of length k. (1)k=0: empty list, l-of-n? returns true. ok. (2)assume true up to k. Consider k+1. By grammar, car must be a number and cdr a list of numbers. cdr of list of k+1 elements has k elements, i.e., l-of-n? can be applied. ok. proof does not discover that pair? is necessary because proof assumes list arguments

plt /8/ nth-elt like list-ref, returns 0..th element of list. (define nth-elt (lambda (list n) (if (null? list) (error 'nth-elt "List too short." (+ n 1) "elements") (if (zero? n) (car list) (nth-elt (cdr list) (- n 1)) ) )

plt /8/ error lists Scheme Request for Implementation $ scheme48 >,open srfi-23 Newly accessible in user: (error) > (define nth-elt … > (nth-elt '(1 2 3) -4) Error: nth-elt "List too short." -6 "elements"

plt /8/ list-length like length, returns number of elements in list (define list-length (lambda (list) (if (null? list) 0 (+ 1 (list-length (cdr list))) ) ) )

plt /8/ fragile vs. robust fragile procedures do not check the types of their arguments. (define list-length ; robust version (lambda (list) (if (list? list) (if (null? list) 0 (+ 1 (list-length (cdr list))) ) (error 'list-length "Not a list:" list) ) ) )

plt /8/ remove-first returns list without first occurrence of symbol list: '()' | '(' symbol '.' list ')' (define remove-first ; fragile (lambda (symbol list) (if (null? list) list … ) ) )

plt /8/ remove-first returns list without first occurrence of symbol list: '()' | '(' symbol '.' list ')' (define remove-first ; fragile (lambda (symbol list) (if (null? list) list (if (eqv? (car list) symbol) (cdr list) … ) )

plt /8/ remove-first returns list without first occurrence of symbol list: '()' | '(' symbol '.' list ')' (define remove-first ; fragile (lambda (symbol list) (if (null? list) list (if (eqv? (car list) symbol) (cdr list) (cons (car list) (remove-first symbol (cdr list)) ) ) ) ) )

plt /8/ remove returns list without all occurrences of symbol list: '()' | '(' symbol '.' list ')' (define remove ; fragile (lambda (symbol list) (if (null? list) list (if (eqv? (car list) symbol) (remove symbol (cdr list)) (cons (car list) (remove symbol (cdr list)) ) ) ) ) )

plt /8/ subst returns s-list with any old replaced by new > (subst 'a 'b '((b c) (b () d))) '((a c) (a () d)) s-list: '(' symbol-expression* ')' symbol-expression: 'Symbol' | s-list s-list: '()' | '(' symbol-expression '.' s-list ')' symbol-expression: 'Symbol' | s-list pair avoids need for another rule

plt /8/ subst s-list: '()' | '(' symbol-expression '.' s-list ')' (define subst ; fragile (lambda (new old slist) (if (null? slist) slist (cons (subst-se new old (car slist)) (subst new old (cdr slist)) ) )

plt /8/ subst-se symbol-expression: 'Symbol' | s-list (define subst-se (lambda (new old se) (if (symbol? se) (if (eqv? se old) new se) (subst new old se) ) ) ) mutual recursion divide-and-conquer because of grammar

plt /8/ subst (define subst ; fragile (lambda (new old slist) (define symbol-expression ; local procedure (lambda (se) ; shares parameters (if (symbol? se) (if (eqv? se old) new se) (subst new old se) ) ) ) (if (null? slist) slist (cons (symbol-expression (car slist)) (subst new old (cdr slist)) ) )

plt /8/ notate-depth returns s-list with symbols annotated for depth > (notate-depth '((b c) (b () d))) '(((b 1) (c 1)) ((b 1) () (d 1))) notate-depth: s-list s-list: '()' | '(' symbol-expression '.' s-list ')' symbol-expression: 'Symbol' | s-list notate-depth needed to mark start at level zero

plt /8/ notate-depth notate-depth: s-list (define notate-depth ; fragile (lambda (slist) (s-list slist 0) )

plt /8/ notate-depth s-list: '()' | '(' symbol-expression '.' s-list ')' (define notate-depth (lambda (slist) (define s-list (lambda (slist d) (if (null? slist) slist (cons (symbol-expression (car slist) d) (s-list (cdr slist) d) ) ) ) ) (s-list slist 0) )

plt /8/ notate-depth symbol-expression: 'Symbol' | s-list (define notate-depth (lambda (slist) (define s-list … (define symbol-expression (lambda (se d) (if (symbol? se) (list se d) (s-list se (+ d 1)) ) ) ) (s-list slist 0) )

plt /8/ notate-depth (define notate-depth (lambda (slist) (define s-list (lambda (slist d) (if (null? slist) slist (cons (symbol-expression (car slist) d) (s-list (cdr slist) d) ) ) ) ) (define symbol-expression (lambda (se d) (if (symbol? se) (list se d) (s-list se (+ d 1)) ) ) ) (s-list slist 0) )

plt /8/ Other Patterns of Recursion l-of-nums: '()' l-of-nums: '(' 'Number' '.' l-of-nums ')' compute sum by structural induction: (define list-sum (lambda (x) (if (null? x) 0 (+ (car x)) (list-sum (cdr x)) ) )

plt /8/ Other Patterns of Recursion (2) vector is not suitable for structural induction, so: (define vector-sum (lambda (v) (partial-vector-sum v (vector-length v)) ) (define partial-vector-sum (lambda (v n) (if (zero? n) 0 (+ (vector-ref v (- n 1)) ; last (partial-vector-sum v (- n 1)) ) )

plt /8/ Other Patterns of Recursion (3) (define vector-sum (lambda (v) (letrec ((partial-vector-sum (lambda (n) (if (zero? n) 0 (+ (vector-ref v (- n 1)) ; last (partial-vector-sum (- n 1)) ))) ) ) (partial-vector-sum (vector-length v)) ) ) ) proof by induction on vector length