Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons.

Similar presentations


Presentation on theme: "Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons."— Presentation transcript:

1 Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License 1

2 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 Function Composition Structural Decomposition Generalization General Recursion Communication via State Module 02

3 Introduction In this lesson, we will talk about Structural Decomposition. This is the strategy you will use to write programs that work on itemized, compound, and mixed data. This is the strategy you will use for the vast majority of your functions. 3

4 Learning Objectives At the end of this lesson, you should be able to apply the structural decomposition strategy to write functions that work on itemized, compound, and mixed data. 4

5 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. Function Composition 2. Structural Decomposition 3. Generalization 4. General Recursion 5. Communication via State

6 Structural Decomposition 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

7 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

8 From Template to Function Definition Recipe for Structural Decomposition 1. Do the first four steps of the design recipe first!! 2. Make a copy of the template and uncomment it 3. Fill in the function name and add more arguments if needed 4. Fill in the blanks in the template with functional compositions of the arguments and the values of the fields. Don't do anything else! 8

9 Video Demonstration: The Traffic Light 9 YouTube link

10 Working with other kinds of data This example showed structural decomposition on itemization data, but other kinds of data are the same: 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. 10

11 What can you put in the blanks? We said: "Fill in the blanks in the template with functional compositions of the arguments and the values of the fields." This means you can build a wiring diagram, where the inputs are the arguments to the function and the values of the fields in the structure. – 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" 11

12 Example ;; book-fn : Book -> ?? (define (book-fn b) (... (book-author b) (book-title b) (book-on-hand b) (book-price b))) 12 ;; book-receipts : ;; Book NonNegInt -> NonNegInt (define (book-fn b sales) (... sales (book-author b) (book-title b) (book-on-hand b) (book-price b))) Template for book Let's say we were designing a two-argument function. When we copied the template and added the extra argument, we would get something that looked like this.

13 What could go in the "..."? Ans: any wiring diagram where the inputs were labeled with sales, (book-author b), (book-title b), (book-on-hand b), and (book-price b). For example: (* sales (book-price b)) 13 * (book-price b) sales

14 "Follow the template" We say that a function definition "follows the template" when it is created by filling in the blanks in the appropriate template. Your code will be judged on whether it follows the template. 14

15 Summary We’ve now seen three Design Strategies: – Function Combination Combine simpler functions in series or pipeline Use with any kind of data – Cases Use with scalar data only – Structural Decomposition Used for enumeration, compound, or mixed data Template gives sketch of function Our most important tool Remember: The shape of the data determines the shape of the program. Remember: The shape of the data determines the shape of the program. 15

16 Next Steps 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. 16


Download ppt "Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons."

Similar presentations


Ads by Google