Abstracting over functions
This section introduces HOF as a way to share code between similar functions. We will investigate two small case studies – dropping a prefix from a list or string and sorting a list.
Dropping a prefix
A frequent data-cleaning task is to drop the leading spaces from a string. This is accomplished by the following function:
dropSpaces :: String -> String dropSpaces [] = [] dropSpaces (c:cs) | c == ' ' = dropSpaces cs | otherwise = c:cs
Recall that strings are just lists of characters. This function uses that fact to perform primitive recursion over the string. It does the job as required:
*Main> dropSpaces " hello" "hello"
This is usually not enough though, as we also want to drop other so-called whitespace characters such as tab and newline. The isSpace :: Char ->...