Currying, partial application and sections
One argument at a time
A Haskell function takes exactly one argument. A type with two arrows, Int -> Int -> Int, is a function that takes one Int and returns a function from Int to Int. The arrow is right associative, so the type is Int -> (Int -> Int), and application is left associative, so add 2 3 is (add 2) 3.
add :: Int -> Int -> Int
add x y = x + y
add 2 3 -- 5
(add 2) 3 -- 5, and add 2 is a function Int -> Int
The two lines are the same expression with the parentheses made explicit. That is the whole of currying: a function of two arguments is a function of one that returns another, and the compiler inserts the intermediate calls for you.
Partial application
Because the intermediate value is a real function, it can be given a name or passed to another function. Supplying fewer arguments than the function takes is called partial application, and it is how most small helpers are written in real code.
addFive :: Int -> Int
addFive = add 5
addFive 10 -- 15
map (add 1) [1, 2, 3] -- [2,3,4]
addFive is not a special construct: it is add 5, the function that remains when the first argument is fixed. map (add 1) does the same thing inline: add 1 is the function that map applies to each element.
Sections
An operator in parentheses with one argument on one side is a section, and it is the shortest way to write a partially applied operator. Put the argument on the right for one order and on the left for the other:
map (* 2) [1, 2, 3] -- [2,4,6]
map (2 *) [1, 2, 3] -- [2,4,6]
map (`div` 2) [4, 6] -- [2,3]
map (subtract 1) [1, 2] -- [0,1]
map (+ 1) [1, 2] -- [2,3]
The first two lines are the same because multiplication is commutative, and that hides the difference: (* 2) is \x -> x * 2 while (2 *) is \x -> 2 * x. The third line uses a backtick section of a named function, (`div` 2), which is \x -> x `div` 2. Subtraction has no commutative disguise: (1 -) is \x -> 1 - x and (subtract 1) is \x -> x - 1. That is why subtract exists as a named function: a left section of - cannot express "one less" with the argument on the right of the operator in the usual reading order.
(1 -) 10 -- -9, the argument is subtracted from 1
subtract 1 10 -- 9, one is subtracted from the argument
Lambdas
A lambda is an anonymous function with the arguments and an arrow: \x -> x * x. It is the general form of a section, and it is what to use when the expression is not a single operator application.
module Main (main) where
main :: IO ()
main = do
print (map (\x -> x * x) [1, 2, 3] :: [Int])
print (filter (\x -> x `mod` 2 == 1) [1 .. 6] :: [Int])
print (zipWith (\a b -> a ++ "-" ++ b) ["x", "y"] ["1", "2"])
putStrLn ((\s -> "name: " ++ s ++ "!") "Ada")
ghc -Wall -O1 -o lambdas lambdas.hs
./lambdas
[1,4,9]
[1,3,5]
["x-1","y-2"]
name: Ada!
The last line is written with a lambda because the body is not a section of one operator: it is concatenation of three parts, which has no single-argument form. When the body is exactly one operator with one missing argument, the section is shorter and the course prefers it; when it is not, the lambda says what it does without inventing a helper. And when the same lambda is used twice, it deserves a name at the top level.
Exercises
- Write double = (* 2) and halve = (`div` 2) and print both on a list.
- Explain the difference between (1 -) and subtract 1 with one example each.
- Write add 2 3 with explicit parentheses around the intermediate call and check that it still builds.
- Replace map (\x -> x * x) with a named helper and with a section, and say which reads best.
- Use a lambda in zipWith to join three lists of words with a separator.
- Write a function of three arguments, partially apply it to one, then to two, and print each result.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a function of type
Int -> Int -> Inttake?Answer
One Int and returns a function from Int to Int
The arrow is right associative, so the type is Int -> (Int -> Int): one argument, then a function.
-
What does
map (add 1) [1, 2, 3]produce whenadd x y = x + y?Answer
[2,3,4]
add 1 is the function that adds one, and map applies it to each element.
-
What is the section
(2 *)?Answer
\x -> 2 * xThe missing argument goes where the hole is, so (2 *) multiplies its argument by two with 2 on the left.
-
What does
(1 -) 10evaluate to?Answer
-9
The section fixes 1 on the left of the subtraction, so the call is 1 - 10.
-
What does
subtract 1 10evaluate to?Answer
9
subtract is flip (-): it subtracts its first argument from its second, so 10 - 1.
-
What does this program print?
module Main (main) where add :: Int -> Int -> Int add x y = x + y main :: IO () main = print (map (add 5) [1, 2, 3] :: [Int])Answer
[6,7,8]
add 5 adds five to each element of the list.
-
Which is a lambda?
Answer
\x -> x + 1A lambda is written with a backslash, its arguments and an arrow; the other three are sections or partial applications.
-
When is a lambda clearer than a section?
Answer
When the body is not a single operator application
A section only fixes one side of one operator; a body with several operations has no section form.
-
What does
map (div2) [4, 6]produce?Answer
[2,3]
The backtick section puts the argument on the left of div, so each element is divided by two.
-
What does this program print?
module Main (main) where main :: IO () main = print (zipWith (\a b -> a ++ "-" ++ b) ["x", "y"] ["1", "2"])Answer
["x-1","y-2"]
The lambda joins the paired elements with a dash, and zipWith stops at the shorter list.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev