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.

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.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
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.:
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 ©
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.
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.
Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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 Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 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.
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.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
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
Examining Two Pieces of Data
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:

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 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 see more common patterns of function definitions that differ only by what functions they call. 2

Learning Objectives At the end of this lesson you should be able to: – recognize the ormap, andmap, and filter patterns – state the contracts for ormap, andmap, and filter, and use them appropriately. – combine these functions using higher-order function combination 3

Let's look at find-dog again ;; find-dog : ListOfString -> Boolean ;; RETURNS: true if "dog" is in the given list. ;; STRATEGY: structure (ListOfString) on los : ListOfString (define (find-dog los) (cond [(empty? los) false] [else (or (string=? (first los) "dog") (find-dog (rest los)))])) (check-equal? (find-dog (list "cat" "dog" "weasel")) true) (check-equal? (find-dog (list "cat" "elephant" "weasel")) false) 4

Here's another function with a similar structure ;; has-even? : ListOfInteger -> Boolean ;; RETURNS: true iff the given list contains ;; an even number ;; STRATEGY: struct. decomp. on ;; los : ListOfInteger (define (has-even? los) (cond [(empty? los) false] [else (or (even? (first los)) (has-even? (rest los)))])) 5

Let's compare (define (find-dog los) (cond [(empty? los) false] [else (or (string=? (first los) "dog") (find-dog (rest los)))])) (define (has-even? los) (cond [(empty? los) false] [else (or (even? (first los) ) (has-even? (rest los)))])) 6

Generalize by adding an argument (define (ormap fn lst) (cond [(empty? lst) false] [else (or (fn (first lst)) (ormap fn (rest lst)))])) As before, we can generalize by adding an argument for the difference. 7

And re-create the originals (define (find-dog lst) (ormap ;; String -> Boolean (lambda (str) (string=? "dog" str)) lst))) (define (has-even? lst) (ormap even? lst)) Again as before, we re- create the originals using our generalized function. If you're afraid of lambda, you can define is-dog? or use a local. But it's good to get comfortable with lambda– it's so useful that it was added to Java as of Java 8. 8 For each of these, the strategy is HOFC

What's the contract for ormap ? Let's see what kind of values each of the pieces of ormap returns. Step through the animation on the next slide to watch this work. 9

What's the contract? ormap : (X -> Bool) ListOf -> Bool (define (ormap fn lst) (cond [(empty? lst) false] [else (or (fn (first lst)) (ormap fn (rest lst)))])) Boolean ormap : (X -> Bool) ListOf -> Bool (define (ormap fn lst) (cond [(empty? lst) false] [else (or (fn (first lst)) (ormap fn (rest lst)))])) ListOf X X X -> Bool ormap : (X -> Bool) ListOf -> Bool (define (ormap fn lst) (cond [(empty? lst) false] [else (or (fn (first lst)) (ormap fn (rest lst)))])) Both branches of the cond return booleans, so ormap must return a Boolean The second argument must be a list of X's. fn must take an X, and it must return a boolean, because its return value is an argument to or. So fn must be a function from X's to Booleans, and lst must be a ListOf. We write all this down in the contract. 10

What's the purpose statement? We’ve written the function definition and the contract, but we won’t be done until we have a purpose statement. Having a purpose statement allows another programmer to use this function without having to look at the code. ;; ormap : (X -> Boolean) ListOf -> Boolean ;; GIVEN: A predicate p on X's and a list of X's lox ;; RETURNS: true iff p holds for at least one item on lox ;; that is, (ormap p (list x_1... x_n)) ;; = (or (p x_1)... (p x_n)) (define (ormap p lox)...) 11

And of course we can do the same thing for and. (define (andmap fn lst) (cond [(empty? lst) true] [else (and (fn (first lst)) (andmap fn (rest lst)))])) 12

Contract and Purpose Statement ;; andmap : (X -> Bool) ListOf -> Bool ;; GIVEN: A predicate p on X's ;; and a list of X's lox ;; RETURNS: true iff p holds for every value ;; on lox ;; that is, (andmap p (list x_1... x_n)) ;; = (and (p x_1)... (p x_n)) The contract and purpose statement look very much like the ones for ormap. 13

Another common pattern Another common list-manipulation problem is to take a list and return a list of those values in the list that pass a certain test. For example, here's a function that returns only the even values in a list of integers. 14

only-evens ;; only-evens ;; : ListOfInteger -> ListOfInteger ;; returns the list of all the even values ;; in the list ;; STRATEGY: Struct. Decomp. on lst : ListOfInteger (define (only-evens lst) (cond [(empty? lst) empty] [else (if (even? (first lst)) (cons (first lst) (only-evens (rest lst))) (only-evens (rest lst)))])) 15

Generalize: filter ;; filter : (X -> Boolean) ListOf ;; -> ListOf ;; RETURNS: the list of all the elements ;; in the list that satisfy the test ;; STRATEGY: Struct. Decomp. on lst : ListOf (define (filter fn lst) (cond [(empty? lst) empty] [else (if (fn (first lst)) (cons (first lst) (filter fn (rest lst))) (filter fn (rest lst)))])) The obvious thing to do here is to replace even? with an extra argument. 16

These can be strung together ;; ListOfInteger -> ListOfInteger ;; RETURNS: the squares of the ;; evens in the given list ;; STRATEGY: HOFC (define (squares-of-evens lon) (map sqr (filter even? lon))) 17 One of the nice things about these functions is that they can be combined using functional composition.

Summary You should now be able to: – recognize the ormap, andmap, and filter patterns – state the contracts for ormap, andmap, and filter, and use them appropriately. – combine these functions using higher-order function combination 18

Next Steps Study 05-3-map.rkt in the examples folder. If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 5.3 Go on to the next lesson 19