are functions that take multiple arguments one at a time instead of as tuples.
Using the fact that functions can return other functions we can define curried functions in two ways:
- a function of multiple arguments that returns a non-function output.
- a function of a single (the leftmost) argument that returns a functions that takes the remaining arguments as arguments. The returned function can itself be defined in these two ways.
Examples
A function that adds two numbers a and b can be defined as
- a function that takes both
aandbas argumentsadd :: (Int -> Int) -> Int add a b = a + b - a function that takes
aas single argument and returns a function that takesbas argument, has a body whereais defined, and returns an intadd :: Int -> (Int -> Int) add a b = a + b