HASKELL / CHAPTER 02
Higher-order functions and composition
IntermediateBuild transformations by connecting compatible function types.
By the end of this chapter
- Read a partially applied function
- Trace composition from right to left
- Use types to reject incompatible stages
The idea
Haskell functions are values, so one function can accept, return, or compose another. A curried function can be partially applied by supplying fewer arguments, producing a new function awaiting the remainder. Read its type from left to right to identify what has already been supplied and what the resulting function still needs.
Composition connects an output type to the next function’s input type. In f . g, g runs conceptually first on the argument and f consumes its result. This is different from writing two operations left to right. Prefer named intermediate functions when a dense composition hides the domain meaning, and use the type signature to make the pipeline’s input and final output explicit.
import Data.Char (toUpper)
normalize :: String -> String
normalize = map toUpper . filter (/= ' ')
main :: IO ()
main = putStrLn (normalize "a b")AB
Filtering removes spaces before map uppercases the remaining characters. The composed function still accepts one String.
Try it yourself
Compose a numeric transformation from two named helpers and give its full type signature.
Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.
CHECK YOUR UNDERSTANDING