Skip to content
LumenaryAcademy
Foundations 55 min

Functions, pipelines and composition

Curried functions, partial application, the pipe operator, recursion and tuples.

Code tested · 3AI-draftedAwaiting review by a computer scientist
MotivationH

Programs as pipelines

Think about tidying a name typed into a form: remove spaces at the ends, make it lower case, split it into words, capitalise each word, join them back together. You can describe the process as a pipeline, each step taking the output of the last. Most data processing has this shape: read, clean, filter, transform, summarise.

F# makes that shape visible in code. Each step is a small function; the pipe operator |> feeds a value into the next function; and composition >> joins functions into a new one. Underneath both is an idea that sounds abstract and turns out to be very practical: every F# function takes exactly one argument. This lesson explains why, and what it lets you do.

DefinitionsH

Curried functions and partial application

When you define let add x y = x + y, F# Interactive reports val add: x: int -> y: int -> int. Ignoring the parameter names, the type is int -> int -> int. Read the arrows from the right, grouped like this: int -> (int -> int). So add is a function that takes an int and returns another function, which takes an int and returns an int. Calling add 2 3 means "apply add to 2, then apply the resulting function to 3".

Definition— Currying and partial application

A curried function takes its arguments one at a time, returning a new function after each. Partial application means supplying only some of the arguments, which gives back a function waiting for the rest. The name honours the logician Haskell Curry.

This is useful because it lets you make specialised functions from general ones without writing new code:

fsharp
// A function of two arguments is really a function that returns a function.
let add x y = x + y
let addTen = add 10            // partial application: supply only the first argument
printfn "%d %d" (add 2 3) (addTen 5)

// Prices with a tax rate: fix the rate once, reuse the function.
let withTax (rate: decimal) (price: decimal) = price * (1m + rate)
let kenyaVat = withTax 0.16m
let indiaGst = withTax 0.18m
printfn "%M %M" (kenyaVat 250m) (indiaGst 250m)

// Lambdas: anonymous functions written with fun.
let applyTwice f x = f (f x)
printfn "%d" (applyTwice (fun n -> n * 3) 2)
printfn "%s" (applyTwice (fun (s: string) -> s + "!") "hey")

// Operators are functions too.
let total = List.fold (+) 0 [ 1; 2; 3; 4 ]
let double = (*) 2
printfn "%d %d" total (double 21)

It prints:

text
5 15
290.00 295.00
18
hey!!
10 42
  • addTen is add with its first argument fixed: a new function of one argument.
  • withTax takes a rate and a price. Fixing the rate gives kenyaVat and indiaGst (16 per cent and 18 per cent, both written as decimals with the m suffix, as in C#). The parameters are annotated as decimal because otherwise F# would assume int for arithmetic.
  • fun n -> n * 3 is a lambda, an unnamed function. applyTwice takes a function as its first argument: a higher-order function.
  • Operators are functions when you wrap them in parentheses: (+) is the function that adds two numbers, so List.fold (+) 0 sums a list, and (*) 2 is "multiply by 2". (Write ( * ) with spaces if you ever need it before a closing bracket, since (* starts a comment.)

The order of parameters matters for partial application, so F# programmers put the parameter that varies least first (the rate) and the "data" last (the price). The whole standard library follows that rule: List.map f list, List.filter predicate list, String.concat separator strings. This is what makes the pipe operator work.

Generic functions for free

What type does F# give applyTwice? It never says what f or x are, only that f is applied to x and then to the result. F# Interactive reports f: ('a -> 'a) -> x: 'a -> 'a (it includes the parameter names). The names with apostrophes, 'a, are type variables: applyTwice works for any type 'a, provided f takes and returns that same type. That is why the program can use it once with numbers and once with strings.

This is called automatic generalisation: whenever nothing in a function's body forces a particular type, F# makes the function generic. In C# you would write the type parameters yourself; in F# they appear on their own. A few more examples, with the types F# infers:

fsharp
let identity x = x                    // 'a -> 'a
let pair x y = (x, y)                 // 'a -> 'b -> 'a * 'b
let twice f = f >> f                  // ('a -> 'a) -> ('a -> 'a)
let firstOf (a, _) = a                // 'a * 'b -> 'a

Arithmetic is the main exception. let add x y = x + y is inferred as working on int, because + must be resolved to one concrete type when the function is compiled. (F# has a mechanism, inline functions, for writing arithmetic that works on every numeric type; the lesson on types shows it.)

Functions that return nothing

A function called only for its effect, such as printing, returns the value (), of type unit, which means "no interesting value". Its type ends in -> unit, as in printfn "%d", of type int -> unit. A function that takes no meaningful input takes () as its argument, as report () did in the previous lesson. If you call a function for its effect and it returns a value you do not need, pass the value to ignore; the compiler warns when a non-unit value is silently discarded, which catches many mistakes.

Worked examplesH

The pipe operator and composition

The pipe operator is defined in one line of FSharp.Core: let (|>) x f = f x. It takes a value on its left and a function on its right, and applies the function to the value. That small change of order turns nested calls inside out, so they read in the order things happen:

fsharp
String.concat " " (Array.map capitalise (words (lower (trim raw))))   // inside out
raw |> trim |> lower |> words |> Array.map capitalise |> String.concat " "   // left to right

Here are both pipelines from the motivation, and a composed function:

fsharp
open System

// Small single-purpose functions...
let trim (s: string) = s.Trim()
let lower (s: string) = s.ToLowerInvariant()
let capitalise (s: string) =
    if s.Length = 0 then s else string (Char.ToUpperInvariant s[0]) + s[1..]
let words (s: string) = s.Split(' ', StringSplitOptions.RemoveEmptyEntries)

// ...combined with the pipe operator: the value flows left to right.
let tidyName (raw: string) =
    raw
    |> trim
    |> lower
    |> words
    |> Array.map capitalise
    |> String.concat " "

printfn "%s" (tidyName "  ngozi   OKONJO-iweala ")
printfn "%s" (tidyName "JOSÉ  saramago")

// The same idea with lists: average of the three best scores.
let scores = [ 71; 94; 58; 88; 99; 64 ]
let bestThreeAverage =
    scores
    |> List.sortDescending
    |> List.truncate 3
    |> List.averageBy float
printfn "Best three average: %.2f" bestThreeAverage

// Composition (>>) builds a new function without naming its argument.
let slug = trim >> lower >> words >> String.concat "-"
printfn "%s" (slug "  Functional Programming in F# ")

It prints:

text
Ngozi Okonjo-iweala
José Saramago
Best three average: 93.67
functional-programming-in-f#

Notice how each step of tidyName is a function of one argument, either directly (trim) or by partial application (Array.map capitalise is Array.map waiting for its array; String.concat " " is waiting for its strings). The pipeline is laid out one step per line, the conventional style.

Real data keeps its surprises: the hyphenated surname came out as "Okonjo-iweala", because the function only capitalises after spaces. Names are notoriously hard to process automatically, and the honest fix is usually to store what people type and only tidy what you must.

Composition. f >> g is a new function that applies f and then g. slug is built entirely from existing functions without mentioning its argument, a style sometimes called point-free. Use it when the result is clearer; when it is not, write a named argument and a pipeline instead. Both forms compile to much the same code.

OperatorMeaningExample
x |> fapply f to x[3; 1; 2] |> List.sort
f >> gfirst f, then gtrim >> lower
g << ffirst f, then g (reads right to left)lower << trim
f <| xapply f to x (low precedence)printfn "%d" <| 2 + 3
Worked examplesH

Recursion and tail calls

Functional code uses recursion where imperative code uses loops. A recursive function must be declared with let rec, so that its name is in scope inside its own body.

fsharp
// Recursion needs 'rec'. Pattern matching on a list: empty, or head :: tail.
let rec sum list =
    match list with
    | [] -> 0
    | head :: tail -> head + sum tail

// Tail-recursive version: the recursive call is the last thing done,
// so the compiler turns it into a loop and deep inputs cannot overflow the stack.
let sumTail list =
    let rec go acc remaining =
        match remaining with
        | [] -> acc
        | head :: tail -> go (acc + head) tail
    go 0L list

printfn "%d %d" (sum [ 1..100 ]) (sumTail [ 1L..1_000_000L ])

// Mutually recursive functions are joined with 'and'.
let rec isEven n = if n = 0 then true else isOdd (n - 1)
and isOdd n = if n = 0 then false else isEven (n - 1)
printfn "%b %b" (isEven 10) (isOdd 7)

// Collatz: how many steps to reach 1?
let collatzSteps start =
    let rec loop n steps =
        if n = 1L then steps
        elif n % 2L = 0L then loop (n / 2L) (steps + 1)
        else loop (3L * n + 1L) (steps + 1)
    loop start 0
printfn "Collatz steps from 27: %d" (collatzSteps 27L)

// Tuples group values; functions can return them and callers can take them apart.
let minMax xs = (List.min xs, List.max xs)
let (low, high) = minMax [ 12; 5; 31; 8 ]
printfn "low=%d high=%d spread=%d" low high (high - low)

let divide a b = (a / b, a % b)
let quotient, remainder = divide 47 5
printfn "47 = 5 * %d + %d" quotient remainder
printfn "%A" (fst (divide 9 2), snd (divide 9 2))

It prints:

text
5050 500000500000
true true
Collatz steps from 27: 111
low=5 high=31 spread=26
47 = 5 * 9 + 2
(4, 1)

Pattern matching on lists. An F# list is either empty, [], or a first element (the head) joined by :: to the rest (the tail). match list with | [] -> ... | head :: tail -> ... handles both cases; the next lesson is all about match.

Tail recursion. In sum, the addition head + sum tail happens after the recursive call returns, so every call needs its own stack frame, and a list of a million elements could overflow the stack. In sumTail, the helper go carries the running total in an accumulator, and the recursive call is the very last thing it does: a tail call. The F# compiler turns such self-calls into a loop, so sumTail handles a million elements in constant stack space. The accumulator pattern (an inner go or loop function with an extra parameter) is the standard way to write efficient recursion in F#. Notice too that the million-element version works with int64 (the L suffix): the total, 500000500000, does not fit in a 32-bit int, and F#'s integer arithmetic, like C#'s, wraps around silently on overflow.

Mutual recursion. Functions that call each other are defined together with and. isEven and isOdd are a classic toy example; real uses include parsers and tree-walkers.

In practice, you will write explicit recursion less often than you might expect, because the library's higher-order functions (List.fold, List.map, Seq.unfold and many more) capture the common patterns. Write recursion for genuinely recursive problems, such as trees, nested data and searches, and when no library function fits.

Tuples

A tuple groups a fixed number of values, possibly of different types: (3, "x") has type int * string. Functions return several values as a tuple (minMax, divide), and callers take them apart with a pattern in a let. Parentheses are optional in that pattern, as let quotient, remainder = ... shows. fst and snd extract the parts of a pair, and %A prints any value, including tuples, in F# syntax.

Tuples are convenient for two or three values used close together. When the meaning of each part matters (is that int * int a width and height, or a minimum and maximum?), use a record, the subject of the next lesson.

Note

Methods in .NET libraries written in C# take their arguments as a tuple, so you call them with parentheses and commas: s.Split(' ', StringSplitOptions.RemoveEmptyEntries). F#'s own functions are curried and take arguments separated by spaces. The difference is visible in the type: string * int -> bool takes a tuple, string -> int -> bool is curried. Both styles meet in everyday F# code.

ExplanationH

Designing with functions

Experienced F# developers share a handful of habits.

Keep functions small and pure where you can. A pure function's result depends only on its arguments, and it changes nothing outside itself. Pure functions can be tested by calling them, combined freely, and run in parallel. Put the impure parts (reading files, calling services, printing, the clock) at the edges of the program, and keep the decisions in the middle pure.

Choose parameter order for piping. Configuration first, data last, so that partial application produces useful functions and pipelines read naturally.

Make illegal calls hard to write. If a function takes three string arguments in a row, callers will eventually swap two of them. Prefer a record or distinct types (the next lessons show how) so the compiler can tell them apart.

Name things that deserve names. A lambda is perfect for a short, obvious step such as fun n -> n * n. When a step needs a comment to explain it, give it a name instead; the name is the comment, and it can be tested on its own.

Don't chase point-free style. A pipeline of named steps is clear; a chain of >> operators mixed with partially applied operators is a puzzle. Clarity wins.

Debugging a pipeline. To see what flows between two steps, select the first few lines and send them to F# Interactive, or temporarily insert a step that prints and passes the value along unchanged:

fsharp
let tap label value =
    printfn "%s: %A" label value
    value

scores |> List.sortDescending |> tap "sorted" |> List.truncate 3
Common misconceptionsH

Common misconceptions

Misconception

"x |> f a passes x as the first argument of f." It passes x as the last argument: x |> f a means f a x. That is why F# functions put their "data" parameter last. Exercise 1 is a trap built on this.

Misconception

"Currying makes function calls slow." The compiler optimises calls with all arguments supplied into ordinary method calls; only partial application creates a function object, and even that is cheap.

Misconception

"All recursion is efficient in F#." Only tail calls are turned into loops. A non-tail-recursive function over a very long list can still overflow the stack. Use an accumulator, or a library function that already does.

Exercises

Exercises

1. Functions, pipelines and composition

CoreComputational

What does this script print?

let subtract x y = x - y
let fromHundred = subtract 100
printfn "%d" (fromHundred 30 |> subtract 5)

2. Functions, pipelines and composition

CoreComputational

What does this script print?

let f = (fun x -> x + 1) >> (fun x -> x * 10)
let g = (fun x -> x + 1) << (fun x -> x * 10)
printfn "%d %d" (f 2) (g 2)

3. Functions, pipelines and composition

Warm-upComputational

What does this script print?

let rec countDigits n = if n < 10 then 1 else 1 + countDigits (n / 10)
printfn "%d" (countDigits 2026 + countDigits 7)

4. Functions, pipelines and composition

CoreComputational

What does this script print?

[ 5; 3; 8; 1; 9; 2 ]
|> List.filter (fun n -> n % 2 = 1)
|> List.map (fun n -> n * n)
|> List.sum
|> printfn "%d"

5. Functions, pipelines and composition

Warm-upComputational

What does this script print?

let swap (a, b) = (b, a)
let point = (3, "x")
let flipped = swap point
printfn "%A" flipped

6. Functions, pipelines and composition

CoreHuman

A function has type decimal -> decimal -> decimal. Which statements are true? (Choose all that apply.)

Choose all that apply

Further reading and resources