Funs: anonymous functions

fun ... end

The fun keyword starts an anonymous function, and end closes it. The arguments sit in parentheses, and the body follows the arrow.

A fun is a value like any other: bind it, pass it, or return it. This module returns one from a named function. Save it as funs.erl.

-module(funs). -export([double/0, adder/1, twice/2, inc/1]). double() -> fun(X) -> X * 2 end. adder(N) -> fun(X) -> X + N end. twice(F, X) -> F(F(X)). inc(X) -> X + 1.

erlc funs.erl
erl

1> F = funs:double(), F(21).
42
2> is_function(F).
true

Apply a fun

Call a fun directly with parentheses. The erlang:apply function does the same with the fun and its arguments as a list.

Direct calls are the everyday form; apply shows up when the argument list is built at runtime.

erl

1> Add = fun(A, B) -> A + B end, erlang:apply(Add, [2, 3]).
5
2> is_function(Add, 2).
true

Capture by name

The fun with a module, function, and arity captures an existing function without calling it. The result is a fun that calls that function.

This is the bridge between named functions and the higher-order tools of lesson 22.

erl

1> Member = fun lists:member/2.
fun lists:member/2
2> Member(2, [1, 2, 3]).
true
3> lists:map(fun funs:inc/1, [1, 2]).
[2,3]

Closures

A fun remembers the variables around it at the moment it was created. The captured values stay alive inside the fun, even after the outer function returns.

Closures keep state without mutation: the captured value is bound once, as ever.

erl

1> Add10 = funs:adder(10), Add10(5).
15
2> N = 10, AddN = fun(X) -> X + N end, AddN(5).
15
3> funs:twice(fun funs:inc/1, 40).
42

Next: Higher-order functions

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the fun keyword build?

    Answer

    An anonymous function

    fun ... end is a function literal: a value with arguments and a body, and no name of its own.

  2. How do you call the fun bound to F with the argument 21?

    Answer

    F(21)

    A bound fun is applied with parentheses, exactly like a named function.

  3. What does is_function(F, 2) check?

    Answer

    That F is a fun of arity 2

    The second argument is the arity: the number of arguments the fun takes.

  4. What does fun lists:member/2 do?

    Answer

    Builds a fun that calls lists:member/2

    The module, function and arity capture a named function as a fun; nothing runs until the fun is called.

  5. What is erlang:apply(F, [2, 3]) equivalent to?

    Answer

    F(2, 3)

    apply takes the fun and the argument list, and calls the fun with those arguments.

  6. What is a closure?

    Answer

    A fun that captures variables from its surroundings

    The fun carries the values it saw when it was created, and uses them on every call.

  7. In Add10 = funs:adder(10), where does the captured value come from?

    Answer

    From the argument N of adder/1

    adder/1 returns a fun that adds its own argument to the N it was called with.

  8. Can a fun change a variable of the function that created it?

    Answer

    No: values are immutable, the fun only reads them

    Erlang has single assignment, so the captured value is fixed; a change means a new binding, not a mutation.

  9. What does funs:double() return?

    Answer

    A fun of arity 1

    double/0 returns fun(X) -> X * 2 end, a fun that takes one argument.

  10. What does funs:twice(fun funs:inc/1, 40) return?

    Answer

    42

    twice/2 applies the fun twice: 40 + 1 then 41 + 1.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev