Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Functions and Pipelines

Currying and Partial Application in F# Explained

Understand why every F# function takes one argument, how currying works, how to create new functions by supplying some arguments, and how to order parameters so partial application is useful.

Here is a surprising fact: in F#, every function takes exactly one argument. A function like add a b is really a function that takes a and returns another function, which takes b. This is called currying, and its practical benefit is partial application: supply some arguments now and get back a ready-to-use function waiting for the rest. This lesson explains both ideas with examples, and shows how to design parameter order so your functions compose nicely.

Currying in one picture

FSHARP
let add a b = a + b

The type is int -> int -> int. The arrow is right-associative, so it means int -> (int -> int): a function from an int to a function from an int to an int. Writing add 2 3 means (add 2) 3.

flowchart LR
    A["add 2 3"] --> B["add 2"]
    B --> C["function: b -> 2 + b"]
    C --> D["applied to 3"]
    D --> E["5"]

Applying a curried function one argument at a time.

You can verify this in FSI:

FSHARP
let addTwo = add 2          // val addTwo: (int -> int)
addTwo 10                   // 12
addTwo 100                  // 102

addTwo is a brand new function made by partially applying add. No special syntax is needed: you just give fewer arguments than the function expects.

Why this is useful

Partial application lets you build specialised functions from general ones:

FSHARP
let multiply factor x = factor * x
let twice = multiply 2
let triple = multiply 3

[ 1; 2; 3 ] |> List.map twice       // [2; 4; 6]
[ 1; 2; 3 ] |> List.map triple       // [3; 6; 9]

Without partial application you would write List.map (fun x -> multiply 2 x). With it, the lambda disappears. The standard library is designed for it: List.map f, List.filter pred and String.concat sep all return functions waiting for the collection.

FSHARP
let onlyEvens = List.filter (fun n -> n % 2 = 0)
let sumOfSquares = List.map (fun n -> n * n) >> List.sum

onlyEvens [ 1; 2; 3; 4 ]          // [2; 4]
sumOfSquares [ 1; 2; 3 ]          // 14

The second function uses >> (composition), which you meet in the next lesson. Notice that neither onlyEvens nor sumOfSquares mentions its list argument: that style is called point-free or tacit programming.

Parameter order matters

Partial application fills parameters from left to right, so put the arguments that change least first, and the "main data" last. That is why F# library functions put the collection at the end: List.map f list, List.filter pred list.

Compare two designs of the same function:

FSHARP
// Data last: partial application works naturally
let addPrefix prefix text = prefix + text
let withDear = addPrefix "Dear "

// Data first: needs a lambda to specialise
let addPrefix' text prefix = prefix + text

With addPrefix you can write names |> List.map withDear. The second design makes the same code awkward. Rule of thumb: configuration first, subject last, so that the pipeline operator |> can feed the subject in.

Tupled functions do not curry

A function that takes one tuple has only one argument, so it cannot be partially applied:

FSHARP
let addT (a, b) = a + b
// addT 2           // error: expected a tuple

Methods from .NET libraries are called with tupled style (String.Join(", ", items)) for the same reason. Wrap them in a curried F# function if you want to use them in pipelines:

FSHARP
let joinWith (sep: string) (items: string list) = System.String.Join(sep, items)
let commaJoin = joinWith ", "
commaJoin [ "a"; "b"; "c" ]       // "a, b, c"

Closures: remembering earlier arguments

The function returned by partial application remembers the arguments you gave. Technically it is a closure: a function bundled with the values it captured.

FSHARP
let makeAdder n = fun x -> x + n
let addTen = makeAdder 10
addTen 5       // 15

makeAdder written this way and let makeAdder n x = x + n behave the same. The compiler turns multi-parameter functions into efficient methods; the "one argument at a time" model is only how you reason about them, so currying does not cost you speed in typical code.

Flipping and wrapping arguments

Sometimes a function has its parameters in the wrong order. Define a tiny helper:

FSHARP
let flip f a b = f b a

let minusFrom = flip (-)       // minusFrom 3 10 = 10 - 3 = 7

Operators can be partially applied by wrapping them in parentheses: (+) 1, (*) 2, and (<) 5. Note that (<) 5 means "5 < x". For the other direction, use a lambda: fun x -> x < 5.

The sprintf trick

sprintf "%s has %d items" is a curried function too. After the format string it waits for a string and an int:

FSHARP
let describe = sprintf "%s has %d items"
describe "cart" 3        // "cart has 3 items"
let describeCart = describe "cart"
describeCart 5           // "cart has 5 items"

Currying in C# terms

C# can imitate currying with nested Func delegates. It works, but the syntax is heavier, which is why partial application is rarer there. The stand-ins below build add, multiply and a prefix function the F# way.

C# C# stand-in: a curried add

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# C# stand-in: specialising with a factor

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is add 2 if let add a b = a + b?

    Applying fewer arguments gives back a function waiting for the rest. That is partial application.
  2. 2. Which parameter should come last for friendly partial application and pipelines?

    Put configuration first and the data last, so partial application creates a reusable function and |> can supply the data.
  3. 3. Why can't you partially apply let addT (a, b) = a + b as addT 2?

    Tupled functions have one parameter, the whole tuple, so there is nothing to apply partially.
  4. 4. What does (+) 1 mean?

    Wrapping an operator in parentheses turns it into a function; supplying 1 leaves one argument open.

Summary

  • Every F# function takes one argument; multi-argument functions are chains of single-argument functions (currying).
  • Supplying fewer arguments than expected produces a new function (partial application).
  • Order parameters with configuration first and data last.
  • Tupled functions are not curried; wrap .NET methods in curried helpers for pipelines.
  • Partial application leads naturally to point-free style and to pipelines.

Next: Pipelines and composition