Function Composition CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.7 1 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons.

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.
Reviewing your Program CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4 © Mitchell Wand, This work is licensed under a Creative Commons.
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Function Composition CS 5010 Program Design Paradigms “Bootcamp” 1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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 ©
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.
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.
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.
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 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.
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © 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.
Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
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.
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.
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.:
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Functions vs. Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.2
The Point of This Course
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
Reviewing your Program
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 5.1
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
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
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:

Function Composition CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License

Learning Objectives At the end of this lesson, the student should be able to define short functions by composing existing functions. 2

Introduction In this lesson, you will learn about Steps 4 and 5 of the design recipe: Design Strategies and Function Definitions. We will start with the simplest design strategy: Function Composition We will learn how to use Function Composition to write function definitions. Other strategies will be covered in subsequent lessons. 3

Programs are sets of Functions We organize our programs as sets of functions. A function takes an argument (or arguments) and returns a result. The contract says what kind of data the argument and result are. Purpose statement describes how the result depends on the argument. The design strategy is a short description of how to get from the purpose statement to the code. 4

The Program Design Strategies 5 Design Strategies 1. Function Composition 2. Structural Decomposition 3. Generalization 4. General Recursion 5. Communication via State

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 6 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. Programs are sets of Functions Design Strategies 1. Function Composition 2. Structural Decomposition 3. Generalization 4. General Recursion 5. Communication via State

Design Strategy #1: Function Composition Used when the answer can be explained as a combination of simpler computations. This is what we did for f2c, where the simpler computations were just arithmetic. 7

Demo: velocity.rkt 8 Note: you should never use Number when you mean Integer, NonNegInt, or Real. Here I should have used Real. YouTube link

Video: area-of-ring 9 I should have used Real (or NonNegReal) here, too. YouTube link

Function Compositions as Diagrams We can think of a function composition as a wiring diagram. The arguments to the function flow in, and then they are steered (piped) through some functions. In the next few slides, we’ll see some of the forms that this can take. For each example, we’ll show a wiring diagram and the corresponding Racket expression. 10

A function call as a wiring diagram f v u (f u v) 11

A simple function composition f v u g (define (my-fcn u v) (g (f u v))) 12

g f h x (define (my-fn x y) (f (g x) (h y)) y Another function composition 13

area-of-ring area-of- circle - inner (define (area-of-ring inner outer) (- (area-of-circle outer) (area-of-circle inner))) outer This is the pattern we used in area-of- ring 14

Wiring Diagram #2 g f h x (define (my-fcn x) (f (g x) (h x)) You can pipe a single argument through two functions. 15

Wiring Diagram #3 g h x (if (p x) (g x) (h x)) p Simple conditionals are also permitted as part of a function composition. 16

Wiring Diagram #4 g f x y (f (g x) y) A function composition can take any wiring diagram as its form, so long as the wiring diagram is small. 17

Definition of a function composition fc ::= variable ::= (function fc fc...) ::= (if (pred var var...) fc fc) But: no conditionals here. Later on, we'll allow more complicated things in place of variables 18

Keep it short! Function composition is for very short definitions only. If you have complicated junk in your function, you must have put it there for a reason. Turn it into a separate function so you can explain it and test it. 19

Bad Example ;; ball-after-tick : Ball -> Ball ;; strategy: structural decomposition (define (ball-after-tick b) (if (and (<= YUP (where b) YLO) (or (<= (ball-x b) XWALL (+ (ball-x b) (ball-dx b))) (>= (ball-x b) XWALL (+ (ball-x b) (ball-dx b))))) (make-ball (- (* 2 XWALL) (ball-x (straight b 1.))) (ball-y (straight b 1.)) (- (ball-dx (straight b 1.))) (ball-dy (straight b 1.))) (straight b 1.))) ;; ball-after-tick : Ball -> Ball ;; strategy: function composition (define (ball-after-tick b) (if (ball-would-hit-wall? b) (ball-after-bounce b) (ball-after-straight-travel b))) Here’s a pair of examples. The one on the left uses structural decomposition, but the principle is the same: Which do you think is clearer? Which looks easier to debug? Which would you like to have to defend in front of a TA? 20

When do you need to introduce help functions? If a function has pieces that can be given meaningful contracts and purpose statements, then break it up and use function composition. Then apply the design recipe to design the pieces. 21

Cases: a small extension to Function Composition Sometimes you need to break up a scalar data type into cases. Do this with a cond. This looks like a template, but it's dealing with scalar data, not itemized data. This is one of the "special-purpose" strategies we mentioned in Lesson 1.1, Slide

Example: 23 ;; f : NonNegReal -> ?? (define (f amt) (cond [(and (<= 0 amt) (< amt 10000))...] [(and (<= amt) (< amt 20000))...] [(<= amt)...])) The predicates must be exhaustive. Make them mutually exclusive when you can.

Fill in the function name, contract, arguments, and strategy ;; tax-on : NonNegReal -> NonNegReal ;; GIVEN: A person’s income ;; PRODUCES: the tax on the income ;; EXAMPLES:.... ;; STRATEGY: Cases on amt (define (tax-on amt) (cond [(and (<= 0 amt) (< amt 10000))...] [(and (<= amt) (< amt 20000))...] [(<= amt)...])) This is contract is sloppy. Currency amounts should never be Real. They should always be integers, and units should be specified. But we don't need to be so careful for this made-up example.

Now fill in the blanks with functional compositions of the arguments ;; tax-on : NonNegReal -> NonNegReal ;; GIVEN: A person’s income ;; PRODUCES: the tax on the income ;; EXAMPLES:.... ;; STRATEGY: Cases on amt (define (tax-on amt) (cond [(and (<= 0 amt) (< amt 10000)) 0] [(and (<= amt) (< amt 20000)) (* 0.10 (- amt 10000))] [(<= amt) (* 0.20 (- amt 20000))])) That's all you need to do!

;; tax-on : NonNegReal -> NonNegReal ;; GIVEN: A person’s income ;; PRODUCES: the tax on the income ;; EXAMPLES:.... ;; STRATEGY: Cases on amt (define (tax-on amt) (cond [(< amt 20000) 0] [(<= amt) (* 0.20 (- amt 20000))])) Often, there are different ways to do this WARNING: This kind of rearrangement is NOT allowed for itemization data. We'll learn about this in Module 2.

Where does cases fit in our menu of design strategies? Cases is a special-purpose strategy, so we didn't list it in our master list of design strategies. We'll see a few more of these. 27

Summary In this lesson, you’ve learned – How to use Function Composition to write a function definition. – When a function definition needs to be simplified by using help functions. – How to use Cases to partition a scalar data type. 28

Next Steps If you have questions or comments about this lesson, post them on the discussion board. 29