1 / 134

Pearls of Functional Algorithm Design

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 .

hea
Télécharger la présentation

Pearls of Functional Algorithm Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pearls of Functional Algorithm Design Chapter 1 Roger L. Costello June 2011

  2. I am reading this book

  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.

  4. What is Functional Algorithm Design? • It is solving problems by composing functions.

  5. Function Composition + toUpper map toUpper map

  6. Function Composition (cont.) “hello world” map toUpper “HELLO WORLD”

  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.

  8. The Problem We Will Solve

  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?

  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?

  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?

  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.

  13. Just the First Difference • We will just find the first difference, not all the differences.

  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

  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

  16. Problem Re-Statement • Find the smallest natural number not in a given finite list of natural numbers.

  17. We Will Solve The Problem In Two Ways

  18. Solution #1

  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

  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, …)

  21. for each x in N x notElem ? no [08, 23, 09, …, 06] discard x yes retain x

  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.

  23. 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, …]

  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: [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

  25. Solution #1 head (filter (notElemxs) [0 ..]) xs (pronounced, ex’es) is the list that we are analyzing. [0 ..] is the natural numbers.

  26. Solution #2

  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.

  28. Example [08, 23, 09, 00, 12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06] length = 20

  29. 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

  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

  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.

  32. zip (cont.) Pair this value with this value

  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)]

  34. repeat • “repeat” is a standard function. • It repeats its argument an infinite number of times. repeat True [True, True, True, …]

  35. 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)]

  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. Association List: [(08, True), (09, True), (00, True), (12, True), (11, True), …, (06, True)]

  37. “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),…]

  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 a value of False.

  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

  40. 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),…]

  41. 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

  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]

  43. 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

  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.

  45. 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.

  46. 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

  47. Hey, that’s the answer! [True, True, True, True, True, …,True] length ___ “15” is the answer to the problem 15

  48. From alist to answer [(0,True),(1,True),(2,True),(3,True),(4,True), …,(14,True),(15,False),(16,False),…] length takeWhileid elems ___ 15

  49. 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

  50. Solution #2 search checklist xs xs (pronounced, ex’es) is the list we are analyzing

More Related