Theory
A higherโorder function (HOF) takes functions as arguments or returns them. Allows us to capture common traversal/computation patterns once and reuse forever.
| HOF | Pattern |
|---|---|
| ๐ฟ Map | apply a transformation to each element |
| ๐ Filter | keep elements satisfying a predicate |
| ๐ Foldrย &ย Foldl | reduce a list using an operator |
| โก๏ธ Function Combinators | glue functions together (.) or swap args (flip) |
Example โ Refactoring with map
shout = map toUpper
squares = map (^2)No explicit recursion needed; clarity & composability improve.
Implementation
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xsCompiler optimizations like fusion rewrite chains (map f . map g) into one traversal.