1.34k likes | 1.35k Views
Pearls of Functional Algorithm Design. Chapter 1. Roger L. Costello June 2011. I am reading this book. Chapter 1. The following slides amplifies the content of the book’s Chapter 1 .
E N D
Pearls of Functional Algorithm Design Chapter 1 Roger L. Costello June 2011
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.
What is Functional Algorithm Design? • It is solving problems by composing functions.
Function Composition + toUpper map toUpper map
Function Composition (cont.) “hello world” map toUpper “HELLO WORLD”
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.
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?
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?
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?
Problem Statement • Find the difference between list A and list B. • List A is in ascending order; list B is in no particular order.
Just the First Difference • We will just find the first difference, not all the differences.
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
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
Problem Re-Statement • Find the smallest natural number not in a given finite list of natural numbers.
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
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, …)
for each x in N x notElem ? no [08, 23, 09, …, 06] discard x yes retain x
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.
Compose filter and notElem [08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06] filter (notElem ___) [0, 1, 2, ..] [15, 16, 18, 20, 22, 24, 25, 26, 27, …]
head • “head” is a standard function. • It selects the first value in a list. • Compose it with filter and notElem to complete the solution: [08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06] head (filter (notElem ___) [0, 1, 2, ..]) 15
Solution #1 head (filter (notElemxs) [0 ..]) xs (pronounced, ex’es) is the list that we are analyzing. [0 ..] is the natural numbers.
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.
Example [08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06] length = 20
Example (cont.) [08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06] x <= no length [08, 23, …, 06] discard x yes retain x
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
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.
zip (cont.) Pair this value with this value
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)]
repeat • “repeat” is a standard function. • It repeats its argument an infinite number of times. repeat True [True, True, True, …]
Create a list of pairs [08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06] zip (filter (<=n) xs) (repeat True) where n = length xs [(08, True), (09, True), (00, True), (12, True), (11, True), …, (06, True)]
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. Association List: [(08, True), (09, True), (00, True), (12, True), (11, True), …, (06, True)]
“OR” each value in the alistwith 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 (||) False (||) False (||) False [(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(15,False),(16,False),…]
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 a value of False.
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
Nearly finished! [08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06] accumArray (||) False (zip (filter (<=n) xs) (repeat True)) where n = length xs [(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(15,False),(16,False),…]
checklist • “checklist” is a (user-defined) function; it is the collection of functions shown on the previous slide (copied below). checklist accumArray (||) False (zip (filter (<=n) xs) (repeat True)) where n = length xs
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]
id • “id” is a standard function. • It is the identity function; give it a value and it returns the same value: True False id id True False
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.
takeWhile (cont.) [True, True, True, True, True, …,True, False, False, True, True] takeWhile id ___ [True, True, True, True, True, …,True] takeWhile stops when it gets to the first False.
length • “length” is a standard function. • It takes one argument, a list; it returns the number of values in the list: [True, True, True, True, True, …,True] length ___ 15
Hey, that’s the answer! [True, True, True, True, True, …,True] length ___ “15” is the answer to the problem 15
From alist to answer [(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(15,False),(16,False),…] length takeWhileid elems ___ 15
search • “search” is a (user-defined) function; it is the collection of functions shown on the previous slide (copied below). search length takeWhile id elems
Solution #2 search checklist xs xs (pronounced, ex’es) is the list we are analyzing