Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 57 ยท Functions

Currying and Partial Application

Learn why every Haskell function takes one argument, how partial application and operator sections work, and when point-free style helps or hurts.

Here is a surprise that shapes all of Haskell: no function takes two arguments. A function that looks like it adds two numbers actually takes one number and returns another function that waits for the second number. This idea is called currying, after the logician Haskell Curry.

It sounds like a trick, but it pays off every day. It lets you build new functions by giving only some of the arguments, and it makes the type signatures you saw in Expressions, Functions and Application easy to read.

Real Haskell is not installed on this site, so the Haskell blocks are read-only. The runnable examples use JavaScript to imitate the idea, and each one says what is real Haskell and what is the stand-in.

One argument at a time

Look at this signature:

HASKELL
add :: Int -> Int -> Int
add x y = x + y

The arrow -> is right-associative, so the type is really Int -> (Int -> Int). Read it as: "add takes an Int and gives back a function from Int to Int." Function application is left-associative, so add 2 3 means (add 2) 3: first apply add to 2, getting a function, then apply that function to 3.

Two arguments? Not quite

Apply to 2 add 2 gives back a function.
Apply to 3 That function adds 2 to its argument.
Result (add 2) 3 is 5.

add 2 3 is just (add 2) 3 without the parentheses.

1 / 2

Partial application

Because add 2 is a perfectly good value, you can name it, pass it around or return it:

HASKELL
addTwo :: Int -> Int
addTwo = add 2

main :: IO ()
main = print (addTwo 10)   -- 12

Giving a function fewer arguments than its signature lists is called partial application. There is no special syntax and no runtime cost to worry about at this level; it is simply ordinary application that happens to stop early.

The pay-off appears with higher-order functions (more in Higher-Order Functions). Instead of writing a lambda, you supply the first arguments:

HASKELL
map (add 10) [1, 2, 3]       -- [11,12,13]
filter (elem 'a') ["cat", "dog", "bat"]   -- ["cat","bat"]

elem 'a' is a function from String to Bool, built by partially applying elem. This works smoothly because library authors put the "configuration" arguments first and the data last. When you design your own functions, copy that habit: put the argument most likely to vary last.

If you cannot decide on the argument order, ask: "Which argument would someone want to fix once and reuse?" That one goes first.

Currying in a stand-in

JavaScript functions are not curried, but we can imitate it by returning functions. This is a stand-in: in Haskell the behaviour is automatic.

JS Currying by hand

Press Run (or Ctrl + Enter) to see the console output here.

No output.

curry2 is what Haskell's standard curry and uncurry relate to. In Haskell you can convert between the two shapes at any time:

HASKELL
curry   :: ((a, b) -> c) -> a -> b -> c
uncurry :: (a -> b -> c) -> (a, b) -> c

uncurry (+) (3, 4)   -- 7

A function taking a tuple, such as (a, b) -> c, really does take one argument (the pair). It cannot be partially applied. That is why idiomatic Haskell prefers the curried form, and why the tuple form shows up mainly when data arrives already paired.

Sections: partial application for operators

Operators are functions with infix syntax, so they can be partially applied too. Wrap an operator and one operand in parentheses to get a section:

HASKELL
(+ 1)      -- \x -> x + 1
(1 +)      -- \x -> 1 + x
(* 2)      -- doubles
(2 ^)      -- \n -> 2 ^ n
(^ 2)      -- \n -> n ^ 2
(`div` 2)  -- halves (integer division)
(subtract 1)  -- \x -> x - 1

The side on which the operand sits decides which argument is fixed. For commutative operators like + it makes no difference; for ^, - and div it matters.

There is one famous exception: (- 1) is not a section, it is the number negative one. Use subtract 1 for "minus one".

JS Sections in JavaScript

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Point-free style

Once partial application and composition are available, you can often drop the final argument from a definition. Compare:

HASKELL
sumOfSquares :: [Int] -> Int
sumOfSquares xs = sum (map (^ 2) xs)

sumOfSquares' :: [Int] -> Int
sumOfSquares' = sum . map (^ 2)

The second form never names xs. It is called point-free (or tacit) style, where "point" means a named argument. The . operator composes functions; it is covered in Lambdas, Composition and the $ Operator.

Point-free code can be elegant when it reads like a pipeline of small steps. It can also become a puzzle:

HASKELL
-- clever, but who wants to debug this?
f = (.) . (.)

A good rule: remove an argument only if the result is easier to read. Naming xs costs nothing, and a named argument makes error messages and debugging simpler.

The eta-reduction rule

Dropping the last argument is allowed when it appears only at the end on both sides. f x = g x is the same as f = g. If the argument is used elsewhere, or is not the last one, you cannot simply remove it:

HASKELL
-- fine:
countWords s = length (words s)   -- countWords = length . words

-- not directly reducible, because y is used in the middle:
greet name = "Hello, " ++ name ++ "!"

The monomorphism restriction

A point-free binding without a type signature can trigger the monomorphism restriction: GHC refuses to generalise a simple pattern binding that has class constraints and picks a default type instead. For top-level definitions, the cure is simple: always write the type signature, as you should anyway.

HASKELL
total = sum . map fromIntegral   -- may fail to be generalised without a signature

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the type Int -> Int -> Int mean when read with explicit parentheses?

    The arrow is right-associative, so the function takes one Int and returns a function from Int to Int.
  2. 2. What is the value of (subtract 1) 10 in Haskell?

    subtract 1 is the function that subtracts one. (- 1) cannot be used for that because it reads as the number negative one.
  3. 3. Which expression is a partial application?

    max needs two arguments. Supplying only one gives a function still waiting for the second.
  4. 4. Why does idiomatic Haskell put the data argument last?

    With the configuration arguments first, you can fix them once and get a reusable function that waits for the data.

Summary

  • Every Haskell function takes exactly one argument; "multi-argument" functions return functions.
  • a -> b -> c means a -> (b -> c), and f x y means (f x) y.
  • Giving fewer arguments than the signature lists is partial application and yields a new function.
  • Operator sections such as (+ 1), (2 ^) and (`div` 2) fix one operand; use subtract 1 instead of (- 1).
  • Point-free style drops the last argument; use it when it improves readability, not to show off, and keep type signatures.

Next: Higher-Order Functions: map, filter, folds.