Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 40 ยท Functions and Recursion

Currying and Partial Application

Understand why OCaml function types have chains of arrows, how every function takes one argument, how to apply some arguments early, and how operators become functions.

When you write let add a b = a + b, the type is int -> int -> int. Why two arrows? Because in OCaml every function takes exactly one argument. A "two-argument function" is a function that takes one argument and returns another function waiting for the second. This is called currying (after the logician Haskell Curry), and it makes partial application a built-in feature.

The idea

These two definitions are identical:

OCAML
let add a b = a + b
let add = fun a -> fun b -> a + b

The type int -> int -> int groups to the right: int -> (int -> int). Applying add to 3 gives a function from int to int:

OCAML
# add 3;;
- : int -> int = <fun>
# (add 3) 4;;
- : int = 7
# add 3 4;;
- : int = 7

Function application add 3 4 associates to the left: (add 3) 4. That is why you do not need parentheses between arguments.

Partial application

Because add 3 is a valid expression, you can give a function only some of its arguments and get back a new function:

OCAML
let add5 = add 5
let increment = add 1

# add5 10;;
- : int = 15
# List.map (add 100) [1; 2; 3];;
- : int list = [101; 102; 103]

This is extremely handy with higher-order functions, where you pass "a function waiting for one more argument":

OCAML
let is_multiple_of d n = n mod d = 0

let evens = List.filter (is_multiple_of 2) [1; 2; 3; 4; 5; 6]
(* [2; 4; 6] *)

is_multiple_of 2 has type int -> bool, exactly what List.filter wants.

Order of parameters matters

To make partial application useful, put the parameter that varies least first and the "data" parameter last. The standard library does this consistently: List.map f xs, List.filter pred xs, String.concat sep parts, so that List.map f is a reusable function:

OCAML
let double_all = List.map (fun x -> 2 * x)

# double_all [1; 2; 3];;
- : int list = [2; 4; 6]

Note that double_all is defined without mentioning the list at all. Beware weak types, however: partial application of a polymorphic function to make a polymorphic result can give a '_weak type, as discussed in Type inference; the fix is to add the list parameter back.

Tupled versus curried

You can define a function over a tuple:

OCAML
let add_t (a, b) = a + b      (* (int * int) -> int *)

This looks like other languages' add(3, 4), but it cannot be partially applied and allocates a tuple (the compiler often optimizes this away). Prefer curried functions unless the pair is a natural unit.

You can convert between the two with tiny helpers:

OCAML
let curry f a b = f (a, b)
let uncurry f (a, b) = f a b

Their types are ('a * 'b -> 'c) -> 'a -> 'b -> 'c and ('a -> 'b -> 'c) -> 'a * 'b -> 'c. Writing them is a good exercise.

Operators are curried functions

Every infix operator is a function in disguise. Wrap it in parentheses to use it as a prefix function:

OCAML
# (+) 1 2;;
- : int = 3
# List.map ((+) 10) [1; 2];;
- : int list = [11; 12]
# List.fold_left (+) 0 [1; 2; 3; 4];;
- : int = 10
# (^) "a" "b";;
- : string = "ab"

Note that ( * ) needs spaces so that (* is not read as a comment start. Subtraction has an extra catch: (-) 1 is a function that subtracts something from 1, while (fun x -> x - 1) is the "decrement". There is no right-section shortcut like in Haskell, so write the lambda.

Putting it together with the pipe

Partial application and |> combine into the typical OCaml pipeline style:

OCAML
let total_of_squares_of_odds xs =
  xs
  |> List.filter (fun n -> n mod 2 = 1)
  |> List.map (fun n -> n * n)
  |> List.fold_left (+) 0

Each stage is a function waiting for the list, which is what the pipe supplies.

Function composition

You can write your own composition operator:

OCAML
let ( >> ) f g x = g (f x)

let add_then_double = (+) 1 >> (fun n -> n * 2)
(* add_then_double 4 = 10 *)

In OCaml 5.x, Fun.compose is available as well, and Fun.id is the identity function.

How is it fast?

You might worry that creating a closure for each argument is slow. The native compiler knows the arity of a function when it is called with all its arguments directly, and it calls it without allocating intermediate closures. The cost appears only when you really partially apply.

A stand-in: currying by hand

The helper below converts a JavaScript function of several arguments into a curried one, so you can feel partial application.

JS Stand-in: curry and partial application

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

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How is the type int -> int -> int grouped?

    The arrow is right associative: a function from int to a function from int to int.
  2. 2. What is add 3 when add : int -> int -> int?

    Giving fewer arguments than the function has produces a new function (partial application).
  3. 3. How do you pass + as a function?

    Parenthesizing an operator turns it into an ordinary prefix function.
  4. 4. Which parameter order is best for partial application?

    Putting rarely-changing arguments first lets you build reusable functions like List.map f.

Summary

  • Every OCaml function takes one argument; multi-argument functions return functions.
  • a -> b -> c means a -> (b -> c); application f x y means (f x) y.
  • Partial application builds new functions from old ones with no extra syntax.
  • Put the data argument last, so functions combine well with |> and List.map.
  • Operators in parentheses are ordinary curried functions.

Next: Higher-order functions