Higher Order Functions Extended Practice

Based off of section 12.5 from the book.

Goal: Create an index for a document: makeIndex :: Doc -> Index

Question: What types should Doc and Index have?

It makes sense to decompose makeIndex into a “pipeline” of steps:

makeIndex :: Doc -> Index
makeIndex
   = lines
   >.> numberLines
   >.> allNumberedWords
   >.> sortWords
   >.> intsToLists
   >.> groupByWord
   >.> eliminateSmallWords
  1. What should be the types for each of these intermediate functions?

  2. lines is a built-in method. What is its type? Does that match our usage of it?

  3. numberLines is supposed to replace each line with the pair of an increasing number and the line. How can we implement that using list functions?

  4. allNumberedWords is supposed to take each line and split it into a list of words, then put those words together with the line’s number. We can split this in steps:

    Write each step.

  5. sortWords sorts the list based on the word comparison.

  6. intsToLists turns each integer into a 1-element list. You can do this via a map.

  7. groupByWord needs to put together the lists corresponding to the same word. You need to actually write a function for that one, with cases for two consecutive elements having th same word.

  8. eliminateSmallWords is a simple filter.