More About Recursive Data Types

Slides:



Advertisements
Similar presentations
Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before.
Advertisements

Binary Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Foldr and Foldl CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.6 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Lists of Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
How to use an Observer Template
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.2
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.2
Case Study: Undefined Variables
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
The Iterative Design Recipe
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
Rewriting your function using map and foldr
Introduction to Universe Programs
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Presentation transcript:

More About Recursive Data Types CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 © Mitchell Wand, 2016-2017 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA

Introduction We've just seen several examples of data definitions for recursive data In this lesson we’ll learn more about the characteristics of a good data type definition and explore how functions on a recursive data type work.

Key Points for Lesson 5.6 At the end of this lesson you should be able to list the properties that a data type with multiple constructors must have. illustrate how a recursive function that follows the observer template always calls itself on smaller and smaller structures.

Multiple constructors In order to define recursive data, we introduced data definitions with multiple constructors, e.g.: ;; CONSTRUCTOR TEMPLATES: ;; -- (make-leaf Number) ;; -- (make-node Tree Tree) ;; CONSTRUCTOR TEMPLATES: ;; empty ;; (cons bs inv) ;; -- WHERE ... CONSTRUCTOR TEMPLATES for NonEmptyXList: -- (cons X empty) -- (cons X NonEmptyXList)

Properties of a good data definition for recursive data There are one or more base cases that do not use recursion The cases are mutually exclusive It is easy to tell the alternatives apart There is one and only one way of building any value. There is an interpretation for each case.

Did our definitions have these properties? Go back now and check to see that our definitions have these properties. All the definitions we (and you) will write will have these properties.

Observer templates always recur on smaller pieces of data Our observer templates embody the principle that the shape of the program follows the shape of the data. This means that our functions always recur on smaller pieces of the data. Let’s first see what that means for lists

The Shape of the Program Follows the Shape of the Data: Lists Xlist xlist-fn is-component-of calls Data Hierarchy (a non-empty Xlist contains another Xlist) Call Tree (xlist-fn calls itself on the component)

Let’s watch this in action: remember nl-sum ;; nl-sum : NumberList -> Number (define (nl-sum lst) (cond [(empty? lst) 0] [else (+ (first lst) (nl-sum (rest lst)))]))

Watch this work: (nl-sum (cons 11 (cons 22 (cons 33 empty)))) = (+ 11 (nl-sum (cons 22 (cons 33 empty)))) = (+ 11 (+ 22 (nl-sum (cons 33 empty)))) = (+ 11 (+ 22 (+ 33 (nl-sum empty)))) = (+ 11 (+ 22 (+ 33 0))) = (+ 11 (+ 22 33)) = (+ 11 55) = 66

Clearly, this function will halt for any NumberList Why? Because at every step it works on a shorter and shorter list, so eventually it reaches empty? and the function halts.

Let’s try something more complicated What about descendant trees? Remember the constructor templates: ;; CONSTRUCTOR TEMPLATES: ;; For Person: ;; (make-person String PersonList) ;; For PersonList: ;; empty ;; (cons Person PersonList)

The Shape of the Program Follows the Shape of the Data: Descendant Trees Person PersonList is-component-of person-fn plist-fn calls Data Hierarchy: Person contains PersonList as a component; and PersonList has Person as a component Call Tree: person-fn calls plist-fn, and plist-fn calls person-fn.

Template: functions come in pairs ;; person-fn : Person -> ?? (define (person-fn p) (... (person-name p) (plist-fn (person-children p)))) ;; plist-fn : PersonList -> ?? (define (plist-fn ps) (cond [(empty? ps) ...] [else (... (person-fn (first ps)) (plist-fn (rest ps)))])) Here is the pair of templates that we get by applying the recipe to our data definition. They are mutually recursive, as you might expect.

Let’s look at our old example again (define alice (make-person "alice" empty)) (define bob (make-person "bob" empty)) (define chuck (make-person "chuck" (list alice bob))) (define dave (make-person "dave" empty)) (define eddie (make-person "eddie" (list dave))) (define fred (make-person "fred" (list chuck eddie))) fred chuck alice bob eddie dave

Let's look at the data structure A Person consists of a String and a PersonList a Person "fred" "chuck" "eddie" "alice" "bob" "dave" a PersonList a String

(person-children p) is always a smaller structure than p. (person-name person1) = "fred" (person-children person1) = "fred" "chuck" "eddie" "alice" "bob" "dave"

The same thing works for NonNegInts ;; sum : ;; NonNegInt NonNegInt -> NonNegInt (define (sum x y) (cond [(zero? x) y] [else (+ 1 (sum (- x 1) y))])

Example (sum 3 2) = (+ 1 (sum 2 2)) = (+ 1 (+ 1 (sum 1 2))) = (+ 1 (+ 1 (+ 1 (sum 0 2)))) = (+ 1 (+ 1 (+ 1 2))) = 5 At every recursive call, the value of the first argument decreases, so eventually it reaches 0.

Fibonacci ;; fib : NonNegInt -> NonNegInt ;; GIVEN: a non-negative integer n ;; RETURNS: the n-th fibonacci number ;; EXAMPLES: ;; fib(0) = 1, fib(1) = 1, fib(2) = 2, fib(3) = 3, ;; fib(4) = 5, fib(5) = 8, fib(6) = 13 ;; STRATEGY: Recur on n-1 and n-2 (define (fib n) (cond [(= n 0) 1] [(= n 1) 1] [else (+ (fib (- n 1)) (fib (- n 2)))])) At every recursive call, the value of n decreases, so eventually it reaches 0 or 1.

Bad Fibonacci ;; bad-fib : NonNegInt -> NonNegInt ;; GIVEN: a non-negative integer n ;; RETURNS: the n-th fibonacci number ;; EXAMPLES: ;; fib(0) = 1, fib(1) = 1, fib(2) = 2, fib(3) = 3, ;; fib(4) = 5, fib(5) = 8, fib(6) = 13 ;; STRATEGY: Recur on n-1 and n-2 (define (bad-fib n) (cond [(= n 0) 1] [(= n 1) 1] [else (+ (bad-fib (- n 1)) (bad-fib (- n 2)))])) At every recursive call, the value of n decreases. But wait! When n = 1, this gets into an infinite loop.

What went wrong? (bad-fib 1) = (bad-fib -1) = (bad-fib -3) = ... The contract says the argument to bad-fib is supposed to be a NonNegInt. When n=1, the call (bad-fib (- n 2)) VIOLATES THE CONTRACT, so all bets are off. It’s really important to make sure that your recursive calls don’t violate the function’s contract. We’ll see much more about this in Module 07.

Summary At the end of this lesson you should be able to list the properties that a data type with multiple constructors must have. illustrate how a recursive function that follows the observer template always calls itself on smaller and smaller structures.

Next Steps If you have questions about this lesson, ask them on the Discussion Board