Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative.

Slides:



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

Function Composition CS 5010 Program Design Paradigms “Bootcamp” 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 ©
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.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
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.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.4 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
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.
Introduction to Universe Programs CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
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
Testing Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
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 2.2
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
Examining Two Pieces of Data
More About Recursive Data Types
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
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 12.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
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
ormap, andmap, and filter
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
Design Strategies 3: Divide into cases
Presentation transcript:

Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License 1

Generalization Over Constants Over Expressions Over Contexts Over Data Representations Over Method Implementations Mixed Data Data Representations Basics Recursive Data Functional Data Objects & Classes Stateful Objects Design Strategies Combine simpler functions Use a template Call a more general function Communicate via State Module 02 2 Divide into Cases

Generalization Over Constants Over Expressions Over Contexts Over Data Representations Over Method Implementations Mixed Data Data Representations Basics Recursive Data Functional Data Objects & Classes Stateful Objects Design Strategies Combine simpler functions Use a template Call a more general function Communicate via State Lesson Divide into Cases

Introduction In this lesson, we will show how to take apart non-scalar data using the destructor template for that type of data. This is the strategy you will use for the vast majority of your functions. 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 5 The Six Principles of this course 1. Programming is a People Discipline 2. Represent Information as Data; Interpret Data as Information 3. Programs should consist of functions and methods that consume and produce values 4. Design Functions Systematically 5. Design Systems Iteratively 6. Pass values when you can, share state only when you must. Design Strategies 1. Combine simpler functions 2. Use template for on 3. Divide into cases on 4. Use HOF on 5. Call a more general function

Use a destructor template Used when the problem can be solved by examining a piece of non-scalar data. Slogan: The shape of the data determines the shape of the program. 6

What does it mean to “examine” a piece of data? If the data is compound data, this means extracting its fields. If the data is itemization data, this means determining which variant the data is. If the data is mixed data, this means determining which variant the data is, and then extracting its fields, if any. Every data definition includes a template that shows how this examination process is to be organized. Writing a function using structural decomposition is accomplished by filling in the blanks in the template. – Definition of "filling in the blank" to come in Slide 11. 7

From Template to Function Definition Recipe for Using a Template 1. Make a copy of the template and uncomment it 2. Fill in the function name and add more arguments if needed 3. The strategy is “Use template for on,” where is the kind of data you are taking apart, and is the variable whose value you are looking at. 4. Fill in the blanks in the template by combining the arguments and the values of the fields using simpler functions. 8

Example: book-receipts ;; book-receipts : Book NonNegInt -> NonNegInt ;; GIVEN: a Book and the number of copies sold ;; RETURNS: the total receipts from the sales of the ;; given book. Ignores the number of copies on hand. ;; EXAMPLE: ;; (book-receipts ;; (make-book "Felleisen" "HtdP2" ) 100) ;; = To do this, we’ll need to look inside the Book to see its price, so we’ll use the Book template

1. Make a copy of the template and uncomment it (define (book-fn b) (... (book-author b) (book-title b) (book-on-hand b) (book-price b))) 10

2. Fill in the function name and add more arguments if needed (define (book-receipts b sales) (... (book-author b) (book-title b) (book-on-hand b) (book-price b))) 11

3. Write down the strategy ;; STRATEGY: Use template for Book on b. (define (book-receipts b sales) (... (book-author b) (book-title b) (book-on-hand b) (book-price b))) 12

4. Fill in the blanks in the template ;; STRATEGY: Use template for Book on b. (define (book-receipts b sales) (* (book-price b) sales)) Things we didn’t use: (book-author b) (book-title b) (book-on-hand b) That’s OK! 13 We said: “4. Fill in the blanks in the template by combining the arguments and the values of the fields using simpler functions.”

Example: next state of traffic light ;; DATA DEFINITION: ;; a TrafficLightState (TLState) is one of ;; -- "red" ;; -- "yellow" ;; -- "green" ;; INTERPRETATION: self-evident 14

Contract and Purpose Statement ;; next-state : TLState -> TLState ;; GIVEN: a TLState ;; RETURNS: the TLState that follows the given TLState ;; EXAMPLES: ;; (next-state "red") = "green" ;; (next-state "yellow") = "red" ;; (next-state "green") = "yellow" 15

1. Make a copy of the template and uncomment it (define (tls-fn state) (cond [(string=? state "red")...] [(string=? state "yellow")...] [(string=? state "green")...])) 16

2. Fill in the function name and add more arguments if needed (define (next-state state) (cond [(string=? state "red")...] [(string=? state "yellow")...] [(string=? state "green")...])) 17

3. Fill in the strategy ;; STRATEGY: Use template for TLState on state (define (next-state state) (cond [(string=? state "red")...] [(string=? state "yellow")...] [(string=? state "green")...])) 18

4. Fill in the blanks ;; STRATEGY: Use template for TLState on state (define (next-state state) (cond [(string=? state "red")...] [(string=? state "yellow")...] [(string=? state "green")...])) 19 What is the answer for “red”?

4. Fill in the blanks ;; STRATEGY: Use template for TLState on state (define (next-state state) (cond [(string=? state "red") "green"] [(string=? state "yellow")...] [(string=? state "green")...])) 20 What is the answer for “red”? Answer (from examples): “green”

4. Fill in the blanks ;; STRATEGY: Use template for TLState on state (define (next-state state) (cond [(string=? state "red") "green"] [(string=? state "yellow") "red"] [(string=? state "green")...])) 21 What is the answer for “yellow”? Answer (from examples): “red”

4. Fill in the blanks ;; STRATEGY: Use template for TLState on state (define (next-state state) (cond [(string=? state "red") "green"] [(string=? state "yellow") "red"] [(string=? state "green") "yellow"])) 22 What is the answer for “green”? Answer (from examples): “yellow”

Working with other kinds of data We've seen how to use templates for compound data and itemization data Mixed data works the same way. Copy the template, uncomment it, and fill in the missing pieces. That's it! If you've thought hard enough about your function, filling in the blanks is easy. 23

What can you put in the blanks? We said: Fill in the blanks in the template by combining the arguments and the values of the fields using simpler functions. This means : – You don't have to use all of the fields – You can use a field twice – You don't have to use the fields "in order" But it has to be simple, as in Lesson

Next Steps Study 02-1-book-receipts.rkt and 02-2-traffic- light.rkt in the Examples folder. – Be sure to finish the previous-state example in traffic-light.rkt If you have questions or comments about this lesson, post them on the discussion board. Do the Guided Practices Go on to the next lesson. 25