Destructor Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.4 1.

Slides:



Advertisements
Similar presentations
Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
Advertisements

Contracts, Purpose Statements, Examples and Tests CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.6 TexPoint fonts used in EMF. Read the TexPoint.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
The Different Kinds of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson
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.:
The Data Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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 ©
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.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
The Data Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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.
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 1: Combine Simpler Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed.
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.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
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.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
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.
Functions vs. Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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
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 4.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.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
Solving Your Problem by Generalization
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
Rewriting your function using map and foldr
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Presentation transcript:

Destructor Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.4 1

Learning Objectives for This Lesson By the time you finish this lesson, you should be able to: – explain what a destructor template is – write destructor templates for typical data. 2

DDR Step 5: Destructor Template The destructor template (or just the template, for short) gives a skeleton for functions that examine or use the data. Once you write the template, writing the function is just a matter of filling in the blanks. This step is a little more complicated than the preceding ones, so we have a recipe for this, too! 3

The template recipe QuestionAnswer 1. Does the data definition distinguish among different subclasses of data? Write a cond with a clause for each subclasses.cond 2. How do the subclasses differ from each other? Use the differences to formulate a condition per clause. 3. Do any of the clauses deal with structured values? If so, add appropriate selector expressions to the clause. 4. 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. 4

Let's see where we are The Function Design Recipe 1. Data Design 2. Contract and Purpose Statement 3. Examples and Tests 4. Design Strategy 5. Function Definition 6. Program Review The Data Design Recipe 1. What information needs to be represented in your program? What kind of information is each piece? 2. Struct Definitions 3. Constructor Template 4. Interpretation 5. Destructor Template 6. Examples 7. Review QuestionAnswer 1. Does the data definition distinguish among different subclasses of data? Write a cond with a clause for each subclasses.cond 2. How do the subclasses differ from each other? Use the differences to formulate a condition per clause. 3. Do any of the clauses deal with structured values? If so, add appropriate selector expressions to the clause. 4. 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. 5

In this lesson QuestionAnswer 1. Does the data definition distinguish among different subclasses of data? Write a cond with a clause for each subclasses.cond 2. How do the subclasses differ from each other? Use the differences to formulate a condition per clause. 3. Do any of the clauses deal with structured values? If so, add appropriate selector expressions to the clause. 4. 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. 6

Lesson Outline In this lesson, we'll learn how to apply the template recipe to itemization, compound, and mixed data. We’ll start with mixed data, and then see how to work out the special cases of compound and itemization data. Let’s start with the BarOrder example. We’ll follow the template recipe. 7

(define-struct coffee (size type milk?)) (define-struct wine (vineyard year)) (define-struct tea (size type)) ;; A BarOrder is one of ;; -- (make-coffee Size Type Boolean) ;; INTERP: ;; size is the size of cup desired ;; type is the origin of the coffee ;; milk? tells whether milk is desired. ;; -- (make-wine Vineyard Year) ;; INTERP: ;; vineyard is the origin of the grapes ;; year is the year of harvest ;; -- (make-tea Size String) ;; INTERP: ;; size is the size of cup desired ;; type is the type of tea (as a string) Data Definition for mixed data: example The structure definitions Here it's clear what the alternatives mean, so all we need to provide is the interpretation of each field in each alternative. 8 Presumably Size and Type are data types defined elsewhere. Presumably Vineyard is also a data type defined elsewhere.

Writing the template for BarOrder ;; bo-fn : BarOrder -> ?? (define (bo-fn order)...) (cond [......] (coffee-size order) (coffee-type order) (coffee-milk? order))] [......] (... (wine-vineyard order) (wine-year order))] [......]) (tea-size order) (tea-type order))])) 9 Start by writing a template for the contract and the beginning of a function definition

Writing the template for BarOrder ;; bo-fn : BarOrder -> ?? (define (bo-fn order) (cond [......] (coffee-size order) (coffee-type order) (coffee-milk? order))] [......] (... (wine-vineyard order) (wine-year order))] [......])) (tea-size order) (tea-type order))])) Write a cond with as many alternatives as the data definition has.

Writing the template for BarOrder ;; bo-fn : BarOrder -> ?? (define (bo-fn order) (cond [(coffee? order)...] (coffee-size order) (coffee-type order) (coffee-milk? order))] [(wine? order)...] (... (wine-vineyard order) (wine-year order))] [(tea? order)...])) (tea-size order) (tea-type order))])) Add predicates that distinguish the different cases

Writing the template for BarOrder ;; bo-fn : BarOrder -> ?? (define (bo-fn order) (cond [(coffee? order) (... (coffee-size order) (coffee-type order) (coffee-milk? order))] [(wine? order) (... (wine-vineyard order) (wine-year order))] [(tea? order) (... (tea-size order) (tea-type order))])) Add selectors to extract the values of the fields. source file: 01-2-template-examples.rkt

What is the destructor template good for? The destructor template (or just the template, for short) gives a skeleton for functions that examine or use the data. The values after the... give us an inventory of the values we can use on the right-hand side of the cond. 13

How to write a template for compound data Just like the one for mixed data, but you don’t need a cond. Here’s an example: 14

Template for compound data (define-struct book (author title on-hand price)) ;; A Book is a ;; (make-book String String NonNegInt NonNegInt) ;; Interpretation: ;; author is the author’s name ;; title is the title ;; on-hand is the number of copies on hand ;; price is the price in USD*100 ;; book-fn : Book -> ?? (define (book-fn b) (... (book-author b) (book-title b) (book-on-hand b) (book-price b))) The constructor template The interpretation of each field The structure definition1.No subclasses, so no cond. 2.The selector functions give you the pieces of data that you can calculate with. 15 source file: 01-2-template-examples.rkt

Template for Itemization Data No selectors, just a cond Here’s a simple example: 16 ;; A Size is one of ;; -- "small" ;; -- "medium" ;; -- "large" ;; size-fn : Size -> ?? (define (size-fn s) (cond [(string=? s "small")...] [(string=? s "medium")...] [(string=? s "large")...]))

Summary You should now be able to write destructor templates for itemization, compound, and mixed data. 17

Next Steps Study the files 01-2-template-examples.rkt in the examples folder. Do Guided Practice 1.2. If you have questions about this lesson, ask them on the Discussion Board Do the Guided Practices Go on to the next lesson 18