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.

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

Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
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.:
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.
CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.
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.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
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.
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.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
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.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.6 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 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 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.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Invariants and Performance 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.
How to Design Worlds CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Searching in a Graph 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.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you.
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.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
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.
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
From Templates to Folds
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Case Study: Undefined Variables
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
Generalizing Similar Functions
The Iterative Design Recipe
Rewriting your function using map and foldr
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Presentation transcript:

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 before you delete this box.: AAA © 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

Introduction In this lesson, we will learn to recognize when your function is suitable for replacement by one of the abstraction functions (map, filter, foldr, etc.) 2

Learning Objectives At the end of this lesson you should be able to: – Use pre-built abstractions for processing lists found in ISL. – Recognize the pre-built abstraction that's appropriate for your function. – Follow a recipe for converting your structural- decomposition into a higher-order function composition. – Follow a recipe for defining a new function using HOFC 3

Introduction There are many ways to generalize functions built using the ListOf template. The textbook refers to these as the list abstractions. We prefer the word generalization, since abstraction can mean many things. Chapter 18 of HtDP/2e gives a helpful list of the built-in list abstractions in ISL. We've seen most of these before, but let's look at them all together. 4

;; map : (X -> Y) ListOf -> ListOf ;; construct a list by applying f to each item of the given ;; list. ;; that is, (map f (list x_1... x_n)) ;; = (list (f x_1)... (f x_n)) (define (map f alox)...) ;; foldr : (X Y -> Y) Y ListOf -> Y ;; apply f on the elements of the given list from right to ;; left, starting with base. ;; (foldr f base (list x_1... x_n)) ;; = (f x_1... (f x_n base)) (define (foldr f base alox)...) Pre-built list abstractions (1) (Chapter 18) The book doesn't use the GIVEN/RETURNS form for purpose statements. But you still need to do so for the functions you write! 5

;; build-list : NonNegInt (NonNegInt -> X) -> ListOf ;; construct (list (f 0)... (f (- n 1))) (define (build-list n f)...) ;; filter : (X -> Boolean) ListOf -> ListOf ;; construct the list from all items on alox for which p ;; holds (define (filter p alox)...) Pre-built list abstractions (2) (Chapter 18) 6

;; andmap : (X -> Boolean) ListOf -> Boolean ;; determine whether p holds for every item on alox ;; that is, (andmap p (list x_1... x_n)) ;; = (and (p x_1)... (p x_n)) (define (andmap p alox)...) ;; ormap : (X -> Boolean) ListOf -> Boolean ;; determine whether p holds for at least one item on alox ;; that is, (ormap p (list x_1... x_n)) ;; = (or (p x_1)... (p x_n)) (define (ormap p alox)...) Pre-built list abstractions (3) (Chapter 18) 7

Which of these should I use? We can write a recipe to help you decide which of these functions to use, if any. 8

Recipe for Rewriting your function to use the Pre-Built List Abstractions 1. Start with your function definition, written using structural decomposition 2. Determine whether the function is a candidate for using one of the built-in abstractions 3. Determine which built-in abstraction is appropriate 4. Rewrite your function using the built-in abstraction. The new strategy is Higher-Order Function Composition 5. Comment out the old definition. Do not change the contract, purpose statement, examples, or tests. Recipe for Rewriting Your Function to Use the Pre-Built List Abstractions 9

A candidate for map or foldr looks like this: (define (f lst a b c) (cond [(empty? lst)...] [else (... (first lst) (f (rest lst) a b c))])) takes a list and some other arguments recurs on the rest of the list; other arguments don't change Here are the things that make your structural decomposition a candidate for one of the list abstractions. 10

A candidate for map looks like this: (define (f lst a b c) (cond [(empty? lst) empty] [else (cons (... (first lst) a b c) (f (rest lst) a b c))])) empty here cons here 11

A candidate for andmap looks like this: (define (f lst a b c) (cond [(empty? lst) true] [else (and (... (first lst) a b c) (f (rest lst) a b c))])) true here and here 12

A candidate for ormap looks like this: (define (f lst a b c) (cond [(empty? lst) false] [else (or (... (first lst) a b c) (f (rest lst) a b c))])) false here or here 13

A candidate for foldr looks like this: (define (f lst a b c) (cond [(empty? lst)...] [else (... (first lst) (f (rest lst) a b c))])) and none of the above patterns ( map, andmap, ormap ) apply. 14

A candidate for filter looks like this: (define (f lst a b c) (cond [(empty? lst) empty] [else (if (... (first lst) a b c) (cons (first lst) (f (rest lst) a b c)) (f (rest lst) a b c))])) takes a list and some other arguments recurs on the rest of the list; other arguments don't change makes a decision based only on the first of the list if test is true, includes the first element in the answer 15

Patterns for Higher-Order Function Composition We've looked at which structural decompositions might be rewritten as higher- order function compositions. Next we'll look at what the function will look like when it's rewritten. The exact form of the rewritten definition will be different for each of the abstraction functions (map, filter, foldr, etc.). Lets look at each of those. 16

General pattern for higher-order function composition ;; list-fn : ListOf -> ?? (define (list-fn lst) (local (; contract for combiner (define (combiner...)...)) (abstraction combiner... lst))) Choose your abstraction from the ones in Section The contract for your combiner depends on which abstraction you choose. The arguments for the different abstractions are different. If this were foldr, the base would go here. The arguments for the different abstractions are different. If this were foldr, the base would go here. 17

Pattern for higher-order function composition: map ;; list-fn : ListOf -> ListOf (define (list-fn lox) (local (; X -> Y ; purpose statement for operator (define (operator x)...)) (map operator lox))) Here is the pattern for a use of map. It is necessary to fill in the right data definitions for X and Y. It isn’t necessary to make the operator a local function or lambda, but if you do, you must write a contract and purpose statement (and examples if they are needed to clarify purpose). 18

Or you could use lambda ;; list-fn : ListOf -> ListOf (define (list-fn lox) (map ; X -> Y ; purpose statement for operator (lambda (x)...)) lox))) 19

Example ;; strategy: structural decomposition on lon : ListOfNumber (define (add-x-to-each x lon) (cond [(empty? lon) empty] [(else (cons (+ x (first lon)) (add1-to-each x (rest lon))))])) ;; strategy: higher-order function composition (define (add-x-to-each x lon) (map ;; Number -> Number (lambda (n) (+ x n)) lon)) Here's an original function, and what we get after we've converted it to use map. Here I've used lambda, but that isn't necessary. This one is so simple you don't need a purpose statement for the lambda. 20

Pattern for higher-order function composition: filter ;; list-fn : ListOf -> ListOf (define (list-fn lox) (local (; X -> Boolean ; purpose statement for test (define (test x)...)) (filter test lox))) Similarly, here is the pattern for a use of filter. 21

Or you could use lambda ;; list-fn : ListOf -> ListOf (define (list-fn lox) (filter ; X -> Boolean ; purpose statement for the test (lambda (x)...) lox)) 22

Pattern for higher-order function composition: andmap/ormap ;; list-fn : ListOf -> Boolean (define (list-fn lox) (local (; X -> Boolean ; purpose statement for test (define (test x)...)) (andmap/ormap test lox))) Either andmap or ormap. 23

Higher-order function composition using lambda : andmap/ormap ;; list-fn : ListOf -> Boolean (define (list-fn lox) (andmap/ormap ; X -> Boolean ; purpose statement for the test (lambda (x)...) lox)) 24

Pattern for higher-order function composition: foldr ;; list-fn : ListOf -> Y (define (list-fn lox) (local (; X Y -> Y ; purpose statement for combiner (define (combiner x y)...)) (foldr combiner base-val lox))) Finally, here is the pattern for a use of foldr. 25

Higher-order function composition using lambda : foldr ;; list-fn : ListOf -> Y (define (list-fn lox) (foldr ; X Y -> Y ; purpose statement for combiner (lambda (first-elt ans-for-rest)...) base-val lox)) These variable names remind you where the values come from. 26

Recognizing Opportunities for HOFC Once you get the idea, you can anticipate when you can use HOFC, and apply it directly. If your function treats all members of the list in the same way, then your function is a candidate. – remove-evens --Yes, all elements are included if they are even. – count-trues --Yes, all elements are counted if they are true. – remove-first-even --No, elements after the first even are treated differently. 27

What’s the strategy? From now on, structural decomposition includes HOFC. So when we write (… (robot-x r) (robot-history r)) we mean: “any FC or HOFC of (robot-x r) and (robot-history r). You could write “SD on r:Robot + HOFC” or “HOFC + SD on r:Robot”, but you don’t have to. 28

Which abstraction function should I use? Key: look at the contracts Here's a recipe, followed by a video demonstration 29

Recipe for Using Higher-Order Function Composition 1. Write the contract, purpose statement, and examples for your function. Does your function process a list? Does it treat all members of the list in more or less the same way? 2. Choose a function from Chapter 18 whose contract matches yours. What choices for X, Y, etc. match your contract? 3. Create a copy of the pattern for the function. What is the contract for the combiner? What is its purpose? 4. Define the combiner function. 5. Test as usual. 30

Video Demonstration: Using HOFC (part 1) 31 YouTube link

Video Demonstration: Using HOFC (part 2) 32 YouTube link

Summary You should now be able to: – Use pre-built abstractions for processing lists found in ISL. – Recognize the pre-built abstraction that's appropriate for your function. – Follow the recipe for converting your structural- decomposition into a higher-order function composition. 33

Next Steps If you have questions about this lesson, ask them on the Discussion Board Do the Guided Do Problem Set 4.Problem Set 4 34