More About Recursive Data Types

Similar documents
Halting Measures and Termination Arguments

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 4.1

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

Solving Your Problem by Generalization

ormap, andmap, and filter

Rewriting your function using map and foldr

Observer Templates. CS 5010 Program Design Paradigms Lesson 1.4

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 6.5

From Templates to Folds

Trees. CS 5010 Program Design Paradigms Lesson 6.2

CS 5010 Program Design Paradigms Lesson 6.1

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Case Study: Free Variables

Case Study: Undefined Variables

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

The Data Design Recipe

Contracts, Purpose Statements, Examples and Tests

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Repetition Through Recursion

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

foldr CS 5010 Program Design Paradigms Lesson 5.4

CS450 - Structure of Higher Level Languages

SCHEME AND CALCULATOR 5b

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

Binary Search. CS 5010 Program Design Paradigms Bootcamp Lesson 8.2

Working with images and scenes

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Doing it in Java. CS 5010 Program Design Paradigms Bootcamp Lesson 5.4

Principles of Programming Languages

Basics of Inheritance

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Intro. Scheme Basics. scm> 5 5. scm>

CS1 Recitation. Week 2

Discussion 11. Streams

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

(Provisional) Lecture 32: Estimated Value, Minimax, Functional Data 10:00 AM, Nov 20, 2017

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

The Art of Recursion: Problem Set 10

CS 275 Name Final Exam Solutions December 16, 2016

Module 10: General trees

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Nonvisual Arrays and Recursion. by Chris Brown under Prof. Susan Rodger Duke University June 2012

CS115 - Module 4 - Compound data: structures

Module 5: Lists. Readings: HtDP, Sections 9, 10.

The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

CS61A Notes Disc 11: Streams Streaming Along

Recursion and Structural Induction

CS1102: Macros and Recursion

Module 05: Types of recursion

CSCC24 Functional Programming Scheme Part 2

RECURSION AND LINKED LISTS 3

INTERPRETERS AND TAIL CALLS 9

Module 9: Binary trees

6.001 Notes: Section 8.1

Software Development. Modular Design and Algorithm Analysis

Using Inheritance to Share Implementations

Hints for Exercise 4: Recursion

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

CS115 - Module 10 - General Trees

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

For this chapter, switch languages in DrRacket to Advanced Student Language.

Unit 8: Recursion. Dave Abel. April 6th, 2016

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

Lab 7: OCaml 12:00 PM, Oct 22, 2017

ITERATORS AND STREAMS 9

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Laboratory: Recursion Basics

Graphs. Directed graphs. Readings: Section 28

Module 8: Binary trees


An introduction to Scheme

Trees. Readings: HtDP, sections 14, 15, 16.

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

How to Design Programs

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

The Design Recipe Fall 2017

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

P1 Engineering Computation

CS 4349 Lecture September 13th, 2017

Model-View-Controller Architecture

Transcription:

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

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

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

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

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

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

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 7

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

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

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 10

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

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

The Shape of the Program Follows the Shape of the Data: Descendant Trees is-component-of calls Person PersonList person-fn plist-fn is-component-of 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. 13

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

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 chuck fred eddie "fred" (list chuck eddie))) alice bob dave 15

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

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

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

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

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

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

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

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

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