Higher-Order Functions on Lists I: Map and Filter
Check out map and filter - two higher-order functions commonly used on lists.
We'll cover the following...
In the previous lessons, we learned how to write recursive functions on lists. There are a few recursion patterns that occur again and again. They can be abstracted in higher-order functions on lists. This allows us to write expressive list functions without worrying too much about implementing explicit recursion.
Map
The first recursion pattern we will look at is transforming list elements. For example, we might want to round each double in a list to its nearest integer.
roundList :: [Double] -> [Int]roundList [] = []roundList (x:xs) = round x : roundList xsmain = print (roundList [1.3, 3.7, 4.0484])
Or we might want to lowercase each character in a string.
import Data.CharlowerString :: [Char] -> [Char]lowerString [] = []lowerString (x:xs) = toLower x : lowerString xsmain = print (lowerString "HELLO, HOW ARE YOU?")
Both specific functions follow the same general pattern:
- They act on a list with some element type
a
and return another list with some element typeb
. - They apply a function of type
a -> b
(e.g.round
,toLower
) element wise to the list.
This pattern is captured by the general function map
, available in the Haskell Prelude. Its implementation is
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = f x : map f xs
Using the higher-order function map
we can write more concise versions of roundList
and lowerString
: