Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.4 1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:

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.
Binary Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
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.:
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.:
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.:
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.
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.
Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
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.
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.
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.
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
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
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.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
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
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
Design Strategies 3: Divide into cases
Presentation transcript:

Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 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

Empty lists Most computations on lists make sense on empty lists – (sum empty) = 0 – (product empty) = 1 – (double-all empty) = empty – etc, etc. 2

Non-empty lists But some computations don't make sense for empty lists – min, max – average 3

Non-Empty Lists For these problems, the list template doesn't make sense, either. For these problems, we use a different data definition and a different template that is tuned for dealing with lists that are always non-empty. 4

Data Definition for Non-Empty List ;; A NonEmptyListOfSardines is one of ;; -- (cons Sardine empty) ;; -- (cons Sardine ;; NonEmptyListOfSardines) 5

Template for Non-Empty List ;; nelist-fn : NonEmptyListOfSardines -> ?? (define (nelist-fn ne-lst) (cond [(empty? (rest ne-lst)) (... (first ne-lst))] [else (... (first ne-lst) (nelist-fn (rest ne-lst)))])) 6 (rest ne-lst) is a NonEmptyListOfSardines so call nelist-fn on it (rest ne-lst) is a NonEmptyListOfSardines so call nelist-fn on it

Template Questions for Non-Empty Lists ;; nelist-fn : NonEmptyListOfSardines -> ?? (define (list-fn ne-lst) (cond [(empty? (rest ne-lst)) (... (first ne-lst))] [else (... (first ne-lst) (list-fn (rest ne-lst)))])) 7 What is the answer for a list of length 1? If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?

Non-Empty Lists: The General Pattern A NonEmptyListOfX is one of -- (cons X empty) interp: a list with a single X -- (cons X NonEmptyListOfX) interp: (cons x lst) represents a sequence whose first element is x and whose other elements are represented by lst. 8

Template Questions for Non-Empty Lists ;; nelist-fn : NonEmptyListOfX -> ?? (define (list-fn ne-lst) (cond [(empty? (rest ne-lst)) (.... (first ne-lst))] [else (... (first ne-lst) (list-fn (rest ne-lst)))])) 9 What is the answer for a list of length 1? If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?

Example: max ;; list-max : NonEmptyListOfInteger -> Integer ;; GIVEN: a non-empty list of integers, ;; RETURNS: the largest element of the list (define (list-max ne-lst) (cond [(empty? (rest ne-lst)) (first ne-lst)] [else (max (first ne-lst) (list-max (rest ne-lst)))])) 10

Example: average lon-avg : LON -> Number Given a non-empty LON, returns its average (lon-avg (cons 11 empty)) = 11 (lon-avg (cons 33 (cons 11 empty))) = 22 (lon-avg (cons 33 (cons 11 (cons 11 empty)))) = 55/3 11

Example: average ;; lon-avg : NELON -> Number ;; Given a non-empty LON, returns its average ;; strategy: structural decomposition (define (lon-avg ne-lst) (cond [(empty? (rest ne-lst)) (first ne-lst)] [else (.... (first ne-lst) (lon-avg (rest ne-lst)))])) 12 If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?

Oops... (lon-avg (list )) = 55/3  ( ) = 55/3 (lon-avg (list 33 11)) = 22  ( ) = 22 Can't have both! Here are two lists. They have the same first element (33), and the average of their rests is the same (11). But they have different averages. So there's no way to combine 33 and 11 that will give the right answer for both examples. So simply using the template can't possibly work.

Try something simpler! lon-avg : NELON -> Number Given a non-empty LON, returns its average Strategy: combine simpler functions (define (lon-avg lst) (/ (lon-sum lst) (lon-length lst))) 14 Here we had a problem that could not be solved by blindly following the template. But we could still solve it by dividing it into simpler pieces and combining the answers for the pieces.

Another way of defining non-empty lists A NonEmptyListOfX is a (cons X ListOfX) 15

When to use this one? Use this one when the first element of the list needs to be treated specially. This one is most often useful with a help function that takes an X and a ListOfX's. We'll see this again in Module 7 when we talk about accumulators and generalizing with invarariants. 16

Remember, don't use non-empty lists unless you really need to The vast majority of problems make sense for the empty list. Make your data definitions in the form ListOfX if that make sense (even if the list in the problem never happens to be empty). If you're using a NonEmptyListOfX template, and you have duplicated code, that's a sign that it should be a plain old ListOfX. 17

Summary You should now be able to explain the difference between a list of items and a non- empty list of items You should be able to write down the template for a non-empty list and use it. 18

Next Steps If you have questions about this lesson, ask them on the Discussion Board Do Problem Set 04 19