![Page 1: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/1.jpg)
1
Pearls of Functional Algorithm Design
Chapter 1
Roger L. CostelloJune 2011
![Page 2: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/2.jpg)
2
I am reading this book
![Page 3: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/3.jpg)
3
Chapter 1
• The following slides amplifies the content of the book’s Chapter 1.
• Chapter 1 shows three ways to solve the problem of finding the smallest free number. Also, it shows a neat sorting algorithm.
• Slides 1 – 57 describes version #1 of finding the smallest free number.
• Slides 58 – 89 describes the sorting algorithm.• Slides 90 – 105 describes version #2 of finding the
smallest free number.• Slides 106 – 133 describes version #3 of finding the
smallest free number.
![Page 4: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/4.jpg)
4
What is Functional Algorithm Design?
• It is solving problems by composing functions.
![Page 5: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/5.jpg)
5
Function Composition
map toUpper
maptoUpper
+
![Page 6: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/6.jpg)
6
Function Composition (cont.)
map toUpper
“hello world”
“HELLO WORLD”
![Page 7: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/7.jpg)
7
Attention
• As you go through these slides, be alert to the functions (puzzle pieces) used.
• Observe how they are composed to solve problems (i.e., how the puzzle pieces are put together to create something new).
• Example: The previous slide composed two functions to solve a problem -- convert strings to uppercase.
![Page 8: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/8.jpg)
8
The Problem We Will Solve
![Page 9: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/9.jpg)
9
Recurring Problem
• Cooking: a recipe calls for this list of ingredients: eggs, flour, milk, chocolate. In my kitchen I have some ingredients. Is there a difference between what the recipe requires versus what I have in my kitchen?
![Page 10: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/10.jpg)
10
Recurring Problem (cont.)
• Product Inventory: the inventory sheet says one thing. The actual products on the shelf says another. Is there a difference between what the inventory sheet says versus what is actually on the shelves?
![Page 11: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/11.jpg)
11
Recurring Problem (cont.)
• Air Mission: the air mission calls for aircraft and weapons. In the military unit there are aircraft and weapons. Is there a difference between what the air mission requires versus what is in the military unit?
![Page 12: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/12.jpg)
12
Problem Statement
• Find the difference between list A and list B.• List A is in ascending order; list B is in no
particular order.
![Page 13: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/13.jpg)
13
Just the First Difference
• We will just find the first difference, not all the differences.
![Page 14: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/14.jpg)
14
Abstract Representation of the Problem
• List A: represent it using the natural numbers, N = (0, 1, …)
• List B: also represent it using the natural numbers; the numbers may be in any order
![Page 15: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/15.jpg)
15
What is the smallest number not in this list?
[08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06]
15
![Page 16: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/16.jpg)
16
Problem Re-Statement
• Find the smallest natural number not in a given finite list of natural numbers.
![Page 17: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/17.jpg)
17
We Will Solve The Problem In Two Ways
![Page 18: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/18.jpg)
18
Solution #1
![Page 19: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/19.jpg)
19
notElem
• “notElem” is a standard function.• It takes two arguments, a value and a list.• It returns True if the value is not an element of
the list, False otherwise.
notElem 23 [08, 23, 09, …, 06]
False
![Page 20: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/20.jpg)
20
notElem (cont.)
• The notElem function can be used to help solve the problem.
• Iterate through each natural number and see if it is not an element of the list. Retain any natural number not in the list.
• See next slide. (Note: “N” denotes the natural numbers: 0, 1, 2, …)
![Page 21: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/21.jpg)
21
for each x in N
notElem?
x
[08, 23, 09, …, 06]nodiscard x
yes
retain x
![Page 22: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/22.jpg)
22
filter
• “filter” is a standard function.• It does all that stuff shown on the previous
slide: – it selects, one by one, the values in N– it hands the value to the notElem function, “Hey, is
this value not an element of [08, 23, 09, …, 06]?” – if notElem returns True, it retains the value.
![Page 23: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/23.jpg)
23
Compose filter and notElem
filter (notElem ___) [0, 1, 2, ..]
[08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06]
[15, 16, 18, 20, 22, 24, 25, 26, 27, …]
![Page 24: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/24.jpg)
24
head
• “head” is a standard function.• It selects the first value in a list.• Compose it with filter and notElem to
complete the solution:
head (filter (notElem ___) [0, 1, 2, ..])
[08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06]
15
![Page 25: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/25.jpg)
25
Solution #1
head (filter (notElem xs) [0 ..])
xs (pronounced, ex’es) is the list that we are analyzing.[0 ..] is the natural numbers.
![Page 26: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/26.jpg)
26
Solution #2
![Page 27: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/27.jpg)
27
Okay to discard some values
• Recall that we are analyzing a list of values.• We are representing the values using numbers.• We represent one value as 0, another value as 2,
and so forth.• Suppose we have 20 values. Suppose one of the
values has the number 23. We can discard it.• Therefore, use the filter function to retain only
values that are less than the length of the list.
![Page 28: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/28.jpg)
28
Example
[08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06]
length = 20
![Page 29: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/29.jpg)
29
Example (cont.)
[08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06]
<=
x
length [08, 23, …, 06]nodiscard x
yes
retain x
![Page 30: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/30.jpg)
30
Example (cont.)
[08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06]
filter (<=n) xs where n = length xs
[08, 09, 00, 12, 11, 01, 10, 13, 07, 04, 14, 05, 17, 03, 19, 02, 06]
These values are discarded: 23, 21
![Page 31: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/31.jpg)
31
zip
• “zip” is a standard function.• It takes two lists and zips them up (just like a
zipper zips up two pieces of clothing). That is, it pairs: – the first value in the first list with the first value in
the second list– the second value in the first list with the second
value in the second list – etc.
![Page 32: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/32.jpg)
32
zip (cont.)
Pair this value with this value
![Page 33: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/33.jpg)
33
zip the filtered set with a list of True values
[08, 09, 00, 12, 11, 01, 10, 13, 07, 04, 14, 05, 17, 03, 19, 02, 06] [True, True, …]
zip
[(08, True), (09, True), (00, True), (12, True), (11, True), …, (06, True)]
![Page 34: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/34.jpg)
34
repeat
• “repeat” is a standard function.• It repeats its argument an infinite number of
times.
repeat True
[True, True, True, …]
![Page 35: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/35.jpg)
35
Create a list of pairs
zip (filter (<=n) xs) (repeat True)where n = length xs
[08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06]
[(08, True), (09, True), (00, True), (12, True), (11, True), …, (06, True)]
![Page 36: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/36.jpg)
36
Association List
• A list of pairs is called an “association list” (or alist for short)
• The first value in a pair is the index. The second value is the value.
• A value can be quickly obtained given its index.
[(08, True), (09, True), (00, True), (12, True), (11, True), …, (06, True)]
Association List:
![Page 37: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/37.jpg)
37
“OR” each value in the alist with False
• For all indexes from 0 to the length of the list, OR the value with False. OR is represented by this symbol: ||
[(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(17,True), (19,True)]
(||) False
[(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(15,False),(16,False),…]
(||) False (||) False………………. (||) False………. ……….
![Page 38: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/38.jpg)
38
Gaps result in the creation of a pair with a value of False
[(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(17,True), (19,True)]
Here’s a gap in the alist.It produces a pair with avalue of False.
![Page 39: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/39.jpg)
39
accumArray
• “accumArray” is a standard function.• It does all the stuff shown on the previous two
slides: For each index from 0 to the length of the list do
Apply a function (e.g., ||) to the value
![Page 40: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/40.jpg)
40
Nearly finished!
accumArray (||) False (zip (filter (<=n) xs) (repeat True)) where n = length xs
[08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06]
[(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(15,False),(16,False),…]
![Page 41: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/41.jpg)
41
checklist
• “checklist” is a (user-defined) function; it is the collection of functions shown on the previous slide (copied below).
accumArray (||) False (zip (filter (<=n) xs) (repeat True)) where n = length xs
checklist
![Page 42: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/42.jpg)
42
elems
• “elems” is a standard function.• Give it an alist and it returns its values:
[(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(15,False),(16,False),…]
elems
[True, True, True, True, True, …,True, False, False, True, True]
![Page 43: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/43.jpg)
43
id
• “id” is a standard function.• It is the identity function; give it a value and it
returns the same value:
True
id
True
False
id
False
![Page 44: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/44.jpg)
44
takeWhile
• “takeWhile” is a standard function.• It takes two arguments, a function and a list; it
starts at the beginning of the list and retains each value until it arrives at a value for which the function returns False.
![Page 45: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/45.jpg)
45
takeWhile (cont.)
takeWhile id ___
[True, True, True, True, True, …,True]
[True, True, True, True, True, …,True, False, False, True, True]
takeWhile stops when it gets to the first False.
![Page 46: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/46.jpg)
46
length
• “length” is a standard function.• It takes one argument, a list; it returns the
number of values in the list:
length ___
15
[True, True, True, True, True, …,True]
![Page 47: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/47.jpg)
47
Hey, that’s the answer!
length ___
15
[True, True, True, True, True, …,True]
“15” is theanswer to
the problem
![Page 48: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/48.jpg)
48
From alist to answer
[(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(15,False),(16,False),…]
length takeWhile id elems ___
15
![Page 49: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/49.jpg)
49
search
• “search” is a (user-defined) function; it is the collection of functions shown on the previous slide (copied below).
length takeWhile id elems
search
![Page 50: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/50.jpg)
50
Solution #2
search checklist xs
xs (pronounced, ex’es) is the list we are analyzing
![Page 51: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/51.jpg)
51
Comparison of the two solutions
![Page 52: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/52.jpg)
52
Solution #1
• With a list of length “n” this solution takes, in the worst case, on the order of n2 steps.
• This will give you an idea of idea how fast the time requirements grow:
n time
1 1
2 4
3 9
4 16
5 25
6 36
![Page 53: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/53.jpg)
53
Solution #2
• With a list of length “n” this solution takes on the order of n steps.
• That is, it is a linear-time solution for the problem. That’s nice!
![Page 54: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/54.jpg)
54
Implementation
![Page 55: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/55.jpg)
55
Haskell
• Haskell is a functional programming language.• The following slides show how to express the
two solutions using Haskell.
![Page 56: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/56.jpg)
56
Solution #1
findGap :: [Int] -> IntfindGap xs = head (filter (`notElem` xs) [0..])
![Page 57: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/57.jpg)
57
Solution #2
import Data.Array
checklist :: [Int] -> Array Int Boolchecklist xs = accumArray (||) False (0,n) (zip (filter (<=n) xs) (repeat True)) where n = length xs
search :: Array Int Bool -> Intsearch = length . takeWhile id . elems
findGap = search . checklist
![Page 58: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/58.jpg)
58
Sort
Problem: Sort a list of values
![Page 59: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/59.jpg)
59
The Problem We Will Solve
![Page 60: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/60.jpg)
60
Recurring Pattern
• Kitchen: In my kitchen I have a 2 quart sauce pan, a 1 quart sauce pan, a 5 quart sauce pan, and another 2 quart sauce pan. I want to organize (sort) them by increasing size.
![Page 61: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/61.jpg)
61
Recurring Pattern
• Bookshelf: On my bookshelf I have a bunch of books. I want to organize (sort) them by author.
![Page 62: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/62.jpg)
62
Problem Statement
• Sort a list of items. There may be duplicates in the list; that’s okay.
![Page 63: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/63.jpg)
63
Abstract Representation of the Problem
• Represent the items in the list using the natural numbers, N = (0, 1, …)
![Page 64: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/64.jpg)
64
Example of sorting a list
[08, 15, 09, 00, 12, 11, 01, 10, 13, 07, 16, 04, 14, 15, 05, 17, 03, 19, 02, 06]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17, 19]
sort
![Page 65: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/65.jpg)
65
Important Assumptions
• Assumption: Each item in the list has a value that is less than the length of the list – On the previous slide the list contains 20 elements.
Thus, each item’s value is 0≤x<20
• Assumption: Duplicates are okay. – On the previous slide there are two occurrences
of 15
![Page 66: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/66.jpg)
66
Time Required
• Given the assumptions on the previous slide, the algorithm shown on the following slides performs a sort in a time proportional to the length of the list.
• That is, the time to sort is linear, i.e., O(n)• That’s fast!
![Page 67: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/67.jpg)
67
Create a list of 1’susing the repeat function
repeat 1
[1, 1, 1, …]
![Page 68: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/68.jpg)
68
Create a list of pairsusing the zip function
[08, 15, 09, 00, 12, 11, 01, 10, 13, 07, 16, 04, …, 19, 02, 06] [1, 1, …]
zip
[(08, 1) , (15, 1), (09, 1), (00, 1), (12, 1), (11, 1), (01, 1), (10, 1)…, (06, 1)]
![Page 69: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/69.jpg)
69
Compose the zip and repeat functions
zip ___ (repeat 1)
[08, 15, 09, 00, 12, 11, 01, 10, 13, 07, 16, 04, 14, 15, 05, 17, 03, 19, 02, 06]
[(08, 1) , (15, 1), (09, 1), (00, 1), (12, 1), (11, 1), (01, 1), (10, 1)…, (06, 1)]
![Page 70: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/70.jpg)
70
Interpret each pair as(index, count)
[(08, 1) , (15, 1), (09, 1), (00, 1), (12, 1), (11, 1), (01, 1), (10, 1)…, (06, 1)]
index
one occurrence
![Page 71: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/71.jpg)
71
Merge pairs with the same index(add their count values)
[(08, 1) , (15, 1), (09, 1), (00, 1), (12, 1), (11, 1), … (15, 1)…, (06, 1)]
(15, 2)
“There are two occurrences of 15”
![Page 72: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/72.jpg)
72
The accumArray Function
• The accumArray function goes through a list of pairs and merges the pairs that have a duplicate index.
• accumArray is flexible in how it merges – you supply it a function and it will use that function to merge the pairs’ values.
• In our problem we supply it the plus (+) function because we want the values added.
![Page 73: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/73.jpg)
73
The accumArray Function (cont.)
• The accumArray function has four arguments. I describe them in reverse order:– A list of pairs, such as that shown two slides back– A pair, (0, n), where n is the length of the list– An initial value for the function (see next)– A function to be applied on the values of pairs with
duplicate indexes
![Page 74: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/74.jpg)
74
The result is an “array”
[(08, 1) , (15, 1), (09, 1), (00, 1), (12, 1), (11, 1), … (15, 1)…, (06, 1)]
accumArray (+) 0 (0, n) (___)where n = length xs
array (0,20) [(0,1),(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),…,(19,1),(20,0)]
![Page 75: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/75.jpg)
75
ComposeaccumArray, zip, and repeat
[08, 15, 09, 00, 12, 11, 01, 10, 13, 07, 16, 04, 14, 15, 05, 17, 03, 19, 02, 06]
accumArray (+) 0 (0, n) (zip xs (repeat 1))where n = length xs
array (0,20) [(0,1),(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),…,(15,2),…,(19,1),(20,0)]
![Page 76: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/76.jpg)
76
countlist
• “countlist” is a (user-defined) function; it is the collection of functions shown on the previous slide (copied below).
accumArray (+) 0 (0, n) (zip xs (repeat 1))where n = length xs
countlist
![Page 77: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/77.jpg)
77
The assocs Function
• The function takes as its argument an array and returns just the list of pairs.
array (0,20) [(0,1),(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),…,(15,2),…,(19,1),(20,0)]
assocs ____
[(0,1),(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),…,(15,2),…,(19,1),(20,0)]
![Page 78: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/78.jpg)
78
Replicate n times the index in (index, n)
[(0,1),(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),…,(15,2),…,(19,1),(20,0)]
(15) (15)
replicate twice
(0) (1)………………………..
replicate oncereplicate once
![Page 79: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/79.jpg)
79
The replicate function
• The replicate function creates n copies of a value. It returns a list, containing n items.
replicate 3 "Ho" returns ["Ho","Ho","Ho"]
replicate 2 15 returns [15,15]
![Page 80: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/80.jpg)
80
Recall “set comprehensions”from your school days
}10,|2{ xNxx
“The set of the first ten even numbers”
A set comprehension builds a more specific set out of a general set. In this example, the more general set is N, the set of natural numbers.
![Page 81: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/81.jpg)
81
Terminology
}10,|2{ xNxx
outputfunction
variableinputset
predicate
![Page 82: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/82.jpg)
82
List Comprehensions
• List comprehensions are similar to set comprehensions.
• The set comprehension on the previous slide is equivalently expressed in Haskell using this list comprehension:
[2*x|x <- [1..10]]
[2,4,6,8,10,12,14,16,18,20]
![Page 83: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/83.jpg)
83
Explanation
[2*x|x <- [1..10]]
“x is drawn from [1 .. 10] and for every value drawn, that value is doubled.”
![Page 84: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/84.jpg)
84
Create a list of the indexes
[(0,1),(1,1),(2,1),(3,1),(4,1),(5,1),…(13,1),(14,1),(15,2),(16,1),(17,1),(18,0),(19,1),(20,0)]
[x | (x,y) <- ____ ]
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
Oops! There should be two of these. Need to replicate.
![Page 85: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/85.jpg)
85
Replicate the indexes the proper number of times
[(0,1),(1,1),(2,1),(3,1),(4,1),(5,1),…(13,1),(14,1),(15,2),(16,1),(17,1),(18,0),(19,1),(20,0)]
[replicate y x | (x,y) <- ____ ]
[[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15,15],[16],[17],[],[19],[]]
Now we need to merge (concat) the list of lists.
![Page 86: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/86.jpg)
86
The concat function
• The concat function creates a single list out of a list of lists
[[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15,15],[16],[17],[],[19],[]]
concat ____
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,16,17,19]
![Page 87: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/87.jpg)
87
array -> sorted list
array (0,20) [(0,1),(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),…,(15,2),…,(19,1),(20,0)]
concat [replicate k x | (x, k) <- assocs (countlist xs)]
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,16,17,19]
![Page 88: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/88.jpg)
88
sort
• “sort” is a (user-defined) function; it is the collection of functions shown on the previous slide (copied below).
concat [replicate k x | (x, k) <- assocs (countlist xs)]
sort
![Page 89: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/89.jpg)
89
Here’s the Solution
import Data.Array
countlist :: [Int] -> Array Int Intcountlist xs = accumArray (+) 0 (0, n) (zip xs (repeat 1)) where n = length xs
sort :: [Int] -> [Int] sort xs = concat [replicate k x | (x, k) <- assocs $ countlist xs]
![Page 90: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/90.jpg)
90
Find the smallest free number
Version #2
![Page 91: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/91.jpg)
91
The Problem We Will Solve
![Page 92: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/92.jpg)
92
Problem Statement
• Find the smallest natural number not in a given finite list of natural numbers.
![Page 93: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/93.jpg)
93
What is the smallest number not in this list?
15
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
![Page 94: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/94.jpg)
94
Important Assumptions
• Assumption: Each item in the list has a value that is less than the length of the list – On the previous slide the list contains 19 elements.
Thus, each item’s value is 0≤x<19
• Assumption: Duplicates are okay. – On the previous slide there are three occurrences
of 14
![Page 95: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/95.jpg)
95
Recall the countlist function
array (0,19) [(0,1),(1,1),(2,1),(3,1),(4,1), … , (14,3),(15,0),(16,0),(17,1),(18,0),(19,1)]
countlist ___
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
See slides 67-76 for an explanation of the countlist function.
![Page 96: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/96.jpg)
96
The countlist function
accumArray (+) 0 (0, n) (zip xs (repeat 1))where n = length xs
countlist
xs is the list of Natural numbers.
![Page 97: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/97.jpg)
97
Recall the checklist function
• The first version (see slides 27-41) to the problem used the checklist function:
checklist xs = accumArray (||) False (0,n) (zip (filter (<=n) xs) (repeat True)) where n = length xs
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
array (0,19) [(0,True),(1,True),(2,True),(3,True),(4,True),…,(14,True),(15,False),(16,False),(17,True),(18,False),(19,True)]
![Page 98: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/98.jpg)
Compare countlist and checklist
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
countlist xs = accumArray (+) 0 (0, n) (zip xs (repeat 1)) where n = length xs
array (0,19) [(0,1),(1,1),(2,1),(3,1),(4,1), … , (14,3),(15,0),(16,0),(17,1),(18,0),(19,1)]
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
checklist xs = accumArray (||) False (0,n) (zip (filter (<=n) xs) (repeat True)) where n = length xs
array (0,19) [(0,True),(1,True),(2,True),(3,True),(4,True),…,(14,True),(15,False),(16,False),(17,True),(18,False),(19,True)]
![Page 99: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/99.jpg)
99
We will use countlistto solve the problem
![Page 100: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/100.jpg)
100
A number that was not in the inputwill have the form (_, 0)
array (0,19) [(0,1),(1,1),(2,1),(3,1),(4,1), … , (14,3),(15,0),(16,0),(17,1),(18,0),(19,1)]
countlist ___
These numbers were not in the input list, as indicated by 0 in the second value of their pairs
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
![Page 101: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/101.jpg)
101
Select all pairs with (_,0)
[(x, k) | (x, k) <- assocs (countlist ___), k == 0]
[(15,0), (16,0), (18,0)]
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
![Page 102: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/102.jpg)
102
Explanation of this list comprehension
[(x, k) | (x, k) <- assocs (countlist xs), k == 0]
input set
predicate
“The (x, k) pairs are drawn from the list of pairs returned by the assocs function (after applying the condition that the second value in each pair equal zero).”
![Page 103: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/103.jpg)
103
Select just the first value in each pair
[x | (x, k) <- assocs (countlist ___), k == 0]
[15, 16, 18]
For each pair, output only the first value.
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
![Page 104: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/104.jpg)
104
Select the first value
head [x | (x, k) <- assocs (countlist ___), k == 0]
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 14, 14, 5, 17, 3, 19, 2, 6]
15
head returns the first item in the list.
![Page 105: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/105.jpg)
105
Here’s the Solution
import Data.Array
countlist :: [Int] -> Array Int Intcountlist xs = accumArray (+) 0 (0, n) (zip xs (repeat 1)) where n = length xs
findGap :: [Int] -> Int findGap xs = head [x | (x, k) <- assocs $ countlist xs, k == 0]
![Page 106: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/106.jpg)
106
Find the smallest free number
Version 3
![Page 107: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/107.jpg)
107
The Problem We Will Solve
![Page 108: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/108.jpg)
108
Problem Statement
• Find the smallest natural number not in given finite list of natural numbers.
![Page 109: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/109.jpg)
109
What is the smallest number not in this list?
15
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
![Page 110: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/110.jpg)
110
Divide and Conquer
• We will split the list in half, determine if the left half contains a missing number and if it does we will recurse on that, otherwise we recurse on the right half.
• We will use the Haskell partition function. It divides a list into a pair consisting of two lists. It has two arguments:– A Boolean function– A list
For each element in the list, if the Boolean function evaluates it to True then it goes in the first list, otherwise it goes in the second list.
partition (<10) [2, 45, 5, 18, 12] returns ([2,5],[45,18,12])
![Page 111: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/111.jpg)
111
Assumption
• The list we are processing has no duplicates• If the list has duplicates, the algorithm may
enter into an infinite recursion
![Page 112: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/112.jpg)
112
Partition using this function: (<b)where b = half the length of the list
[8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
partition (<b) ___ where b = 1 + n `div` 2 n = length xs
[8,9,0,1,7,4,5,3,2,6] [12,11,10,13,14,24,34,17,19]
b==10
![Page 113: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/113.jpg)
113
Does the left list have gaps?
[8, 9, 0, 1, 7, 4, 5, 3, 2, 6]
How do we tell if the list is any missing numbers?Here’s an easy way to tell: 1. Get the length of the list2. If there are any missing numbers then the length is less than b.
Example: on the previous slide b = length xs `div` 2, which is 10. The length of the above list is 10. Thus, it must not have any gaps. Pretty neat, aye?
![Page 114: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/114.jpg)
114
Variable names we will use
xs = [8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
us = [8,9,0,1,7,4,5,3,2,6] vs = [12,11,10,13,14,24,34,17,19]
a = index of the first item
m
n = length xsb = a + 1 + (n `div` 2)m = length us
partition
![Page 115: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/115.jpg)
115
Does the left list have gaps?
xs = [8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
us = [8,9,0,1,7,4,5,3,2,6] vs = [12,11,10,13,14,24,34,17,19]
a = index of the first item
n = length xsb = a + 1 + (n `div` 2)m = length us
partition
If m == b – a then the left list has no gaps
![Page 116: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/116.jpg)
116
If we recurse on the left list …
xs = [8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
xs = [8,9,0,1,7,4,5,3,2,6] vs = [12,11,10,13,14,24,34,17,19]
a = the previous value of a
n = the previous value of m
partition
![Page 117: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/117.jpg)
117
If we recurse on the right list …
xs = [8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
us = [8,9,0,1,7,4,5,3,2,6] xs = [12,11,10,13,14,24,34,17,19]
a = the previous value of b
n = the previous value of n minus the previous value of m
partition
![Page 118: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/118.jpg)
118
Let’s trace an example
• The following slides traces the processing of a list using the divide-and-conquer algorithm.
![Page 119: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/119.jpg)
119
xs = [8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
a = 0n = length xs = 19b = a + 1 + (n `div` 2) = 0 + 1 + (19 `div` 2) = 10
![Page 120: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/120.jpg)
120
xs = [8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
us = [8,9,0,1,7,4,5,3,2,6] vs = [12,11,10,13,14,24,34,17,19]
a = 0n = length xs = 19b = a + 1 + (n `div` 2) = 0 + 1 + (19 `div` 2) = 10
partition (< b) xs
![Page 121: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/121.jpg)
121
xs = [8, 9, 0, 12, 11, 1, 10, 13, 7, 4, 14, 24, 34, 5, 17, 3, 19, 2, 6]
us = [8,9,0,1,7,4,5,3,2,6] vs = [12,11,10,13,14,24,34,17,19]
a = 0n = length xs = 19b = a + 1 + (n `div` 2) = 0 + 1 + (19 `div` 2) = 10
partition (< b) xs
m = length us = 10m == b – a == 10 – 0 == TrueTherefore, this list has no missingnumbers and we should recurse onthe other list, vs.
![Page 122: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/122.jpg)
122
xs = [12,11,10,13,14,24,34,17,19]
set a to the value of b (10)set n to this value: n – m (19 – 10 = 9)
![Page 123: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/123.jpg)
123
xs = [12,11,10,13,14,24,34,17,19]
a = b = 10n = n – m = 19 – 10 = 9
b = a + 1 + (n `div` 2) = 10 + 1 + (9 `div` 2) = 15
![Page 124: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/124.jpg)
124
xs = [12,11,10,13,14,24,34,17,19]
a = b = 10n = n – m = 19 – 10 = 9
b = a + 1 + (n `div` 2) = 10 + 1 + (9 `div` 2) = 15
us = [12,11,10,13,14] vs = [24,34,17,19]
partition (< b) xs
![Page 125: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/125.jpg)
125
xs = [12,11,10,13,14,24,34,17,19]
a = b = 10n = n – m = 19 – 10 = 9
b = a + 1 + (n `div` 2) = 10 + 1 + (9 `div` 2) = 15
partition (< b) xs
m = length us = 5m == b – a == 15 – 10 == TrueTherefore, this list has no missing numbers and we should recurse on the other.
us = [12,11,10,13,14] vs = [24,34,17,19]
![Page 126: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/126.jpg)
126
xs = [24,34,17,19]
set a to the value of b (15)set n to the value of n - m (4)
![Page 127: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/127.jpg)
127
xs = [24,34,17,19]
b = a + 1 + (n `div` 2) = 15 + 1 + (4 `div` 2) = 18
xs = [24,34,17,19]
set a to the value of b (15)set n to the value of n - m (4)
![Page 128: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/128.jpg)
128
us = [17] vs = [24,34,19 ]
partition (< b) xs
m = length us = 1m == b – a == 18 – 15 == FalseTherefore, this list has a missing number and we should recurse on it.
xs = [24,34,17,19]
b = a + 1 + (n `div` 2) = 15 + 1 + (4 `div` 2) = 18
xs = [24,34,17,19]
set a to the value of b (15)set n to the value of n - m (4)
![Page 129: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/129.jpg)
129
xs = [17]
set a to the old value of a (15)set n to the value of m (1)
![Page 130: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/130.jpg)
130
xs = [17]
set a to the old value of a (15)set n to the value of m (1)
b = a + 1 + (n `div` 2) = 15 + 1 + (1 `div` 2) = 16
![Page 131: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/131.jpg)
131
xs = [17]
set a to the old value of a (15)set n to the value of m (1)
b = a + 1 + (n `div` 2) = 15 + 1 + (1 `div` 2) = 16
us = [ ] vs = [17]
partition (< b) xs
m = length us = 0m == b – a == 16 – 15 == FalseTherefore, this list has a missing number and we should recurse on it.
![Page 132: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/132.jpg)
132
If n == 0 then return a
set a to the old value of a (15)set n to the value of m (0)
Done! The answer is: 15
![Page 133: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/133.jpg)
133
Time Requirements
• With a list of length “n” this algorithm takes on the order of n steps.
• That is, it is a linear-time solution for the problem.
![Page 134: Pearls of Functional Algorithm Design Chapter 1 1 Roger L. Costello June 2011](https://reader035.vdocument.in/reader035/viewer/2022062619/5518d1d8550346b31f8b5d1e/html5/thumbnails/134.jpg)
134
Here’s the Solution
import List
minfree :: [Int] -> Intminfree xs = minfrom 0 (length xs, xs)
minfrom :: Int -> (Int, [Int]) -> Intminfrom a (n, xs) | n == 0 = a | m == b - a = minfrom b (n - m, vs) | otherwise = minfrom a (m, us) where (us, vs) = partition (<b) xs b = a + 1 + (n `div` 2) m = length us