Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, 2012-2013 This work is licensed under a Creative.

Slides:



Advertisements
Similar presentations
When do I need an invariant? CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Advertisements

Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
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.:
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
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.:
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
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 ©
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
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.
Function Composition CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
From Templates to Folds CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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.
Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © Mitchell Wand, This work is licensed under.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Finishing the Data Definition Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a.
Examining Two Pieces of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Design Strategies 3: Divide into cases CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
The Point of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables 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.
Destructor Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
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.
The DD  OO Recipe CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.4 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Organization of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.2 © Mitchell Wand, This work is licensed under a Creative Commons.
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.
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.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
How to use an Observer Template
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
More Recursive Data Types
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
The Different Kinds of Data
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
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
Rewriting your function using map and foldr
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Design Strategies 3: Divide into cases
Presentation transcript:

Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.Creative Commons Attribution-NonCommercial 3.0 Unported License 1

Mutually Recursive Data Definitions Sometimes two kinds of data are intertwined In this lesson, we'll consider an easy example: alternating lists An alternating list is a list whose elements alternate between numbers and strings 2

Learning Objectives At the end of this lesson, the student should be able to – recognize information that should be represented as an alternating list – write a data definition for an alternating list – explain why templates for alternating lists come in pairs 3

Alternating Lists Let's write a data definition for lists whose elements alternate between numbers and strings. 4

Data Definitions ;; A ListOfAlternatingNumbersAndStrings (LANS) is one of: ;; -- empty ;; -- (cons Number LASN) ;; A ListOfAlternatingStringsAndNumbers (LASN) is one of: ;; -- empty ;; -- (cons String LANS) A LANS is a list of alternating numbers and strings, starting with a number. A LASN is a list of alternating numbers and strings, starting with a string. Either can be empty. Note that the rest of a non-empty LANS is a LASN, and vice-versa. 5

Examples empty is a LASN (cons 11 empty) is a LANS (cons "foo" (cons 11 empty)) is a LASN (cons 23 (cons "foo" (cons 11 empty))) is a LANS (cons "bar" (cons 23 (cons "foo" (cons 11 empty)))) is a LASN 6

These data definitions are mutually recursive ;; A ListOfAlternatingNumbersAndStrings (LANS) is one of: ;; -- empty ;; -- (cons Number LASN) ;; A ListOfAlternatingStringsAndNumbers (LASN) is one of: ;; -- empty ;; -- (cons String LANS) The definition of a LANS depends on LASN, and the definition of a LASN depends on LANS. 7

This is mutual recursion LASN LANS defined in terms of 8

The template recipe QuestionAnswer Does the data definition distinguish among different subclasses of data? Your template needs as many cond clauses as subclasses that the data definition distinguishes.cond How do the subclasses differ from each other? Use the differences to formulate a condition per clause. Do any of the clauses deal with structured values? If so, add appropriate selector expressions to the clause. Does the data definition use self- references? Formulate ``natural recursions'' for the template to represent the self-references of the data definition. Do any of the fields contain compound or mixed data? If the value of a field is a foo, add a call to a foo-fn to use it. The template recipe doesn't need to change 9

Templates come in pairs ;; lans-fn : LANS -> ?? ;; (define (lans-fn lans) ;; (cond ;; [(empty? lans)...] ;; [else (... ;; (first lans) ;; (lasn-fn (rest lans)))])) ;; lasn-fn : LASN -> ?? ;; (define (lasn-fn lasn) ;; (cond ;; [(empty? lasn)...] ;; [else (... ;; (first lasn) ;; (lans-fn (rest lasn)))])) Here are the templates for LANS and LASN. Observe the recursive calls, in red. 10

Templates are mutually recursive ;; lans-fn : LANS -> ?? ;; (define (lans-fn lans) ;; (cond ;; [(empty? lans)...] ;; [else (... ;; (first lans) ;; (lasn-fn (rest lans)))])) ;; ;; lasn-fn : LASN -> ?? ;; (define (lasn-fn lasn) ;; (cond ;; [(empty? lasn)...] ;; [else (... ;; (first lasn) ;; (lans-fn (rest lasn)))])) 11

This is mutual recursion lasn-fn lans-fn defined in terms of Here's that same picture, this time describing the recursive calls in the template. 12

The template questions ;; lans-fn : LANS -> ?? ;; (define (lans-fn lans) ;; (cond ;; [(empty? lans)...] ;; [else (... ;; (first lans) ;; (lasn-fn (rest lans)))])) ;; ;; lasn-fn : LASN -> ?? ;; (define (lasn-fn lasn) ;; (cond ;; [(empty? lasn)...] ;; [else (... ;; (first lasn) ;; (lans-fn (rest lasn)))])) What is the answer for the empty LANS? If you knew the answer for the LASN inside the LANS, what would the answer be for the whole LANS? What is the answer for the empty LASN? If you knew the answer for the LANS inside the LASN, what would the answer be for the whole LASN? As usual, we have one question for each blank in the template. 13

One function, one task Each function deals with exactly one data definition. So functions will come in pairs Write contracts and purpose statements together, or Write one, and the other one will appear as a wishlist function 14

Example lans-sum : LANS -> Number Returns the sum of all the numbers in the given Lans lasn-sum : LASN -> Number Returns the sum of all the numbers in the given Lasn Here's an example of a pair of functions that should go together. 15

Examples (lans-sum (cons 23 (cons "foo" (cons 11 empty)))) = 34 (lasn-sum (cons "bar" (cons 23 (cons "foo" (cons 11 empty))))) = 34 And here are some examples for our two functions. Observe that lans-sum is applied to a LANS, and lasn-sum is applied to a LASN. 16

Strategy and Function Definitions ;; strategy: structural decomposition on LANS and LASN ;; lans-sum : LANS -> Number (define (lans-sum lans) (cond [(empty? lans) 0] [else (+ (first lans) (lasn-sum (rest lans)))])) ;; lasn-sum : LASN -> Number (define (lasn-sum lasn) (cond [(empty? lasn) 0] [else (lans-sum (rest lasn))])) We apply structural decomposition by filling in each of the four blanks with the answer to the corresponding template question. 17

What are alternating lists good for? ??? Information ??? Alternating Lists representation interpretation 18 Answer: Not much! Don't use them! But they make a good example of mutually- recursive data definitions

Summary You should now be able to: – recognize information that should be represented as an alternating list – write a data definition for an alternating list – explain why templates for alternating lists come in pairs 19

Next Steps If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 6.4 Go on to the next lesson 20