Yes—an Elixir anonymous function can recurse. A direct pattern is to pass the function itself as an argument, then call that argument with dot syntax: recur.(recur, next_value). For a recursive operation used as a normal part of a module’s API, named function clauses are usually clearer.
How to write a recursive anonymous function
Elixir defines an anonymous function with fn ... end and invokes a function value with a dot, as in f.(argument). One way to make such a function recurse is to give it a parameter that holds the function value and pass that parameter back into each recursive call:
As an Amazon Associate I earn from qualifying purchases.
factorial = fn recur, n ->
if n <= 1 do
1
else
n * recur.(recur, n - 1)
end
end
factorial.(factorial, 5)
The call returns 120. Here, recur is not a special keyword or an automatic self-reference; it is an ordinary argument containing the function. Each recursive step supplies it again. This is an illustrative construction from Elixir’s documented function and invocation syntax, not a uniquely canonical or officially recommended idiom. The example treats values at or below 1 as the stopping case; define input validation and desired behavior explicitly if using a similar function in application code.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Why the function has to receive itself
An anonymous fn expression gives you a function value, but it does not create a named binding that the function body can use to refer to itself. Passing that value as an argument provides a reference for the recursive call. The function value must be invoked with dot syntax: recur.(...).
#1 Best Overall
That syntax differs from calling a named function in a module. The official Elixir Function module documentation demonstrates calls such as add.(1, 2) for function values and shows that anonymous functions can have multiple clauses. Clauses in one anonymous function must accept the same number of arguments.
What the capture operator does—and does not do
The & operator has two relevant uses, but neither makes a function refer to itself automatically:
&String.length/1captures an existing named function as a function value.&(&1 + &2)creates a function using argument placeholders.
These forms can be useful for passing functions around, but they do not replace the explicit self-argument pattern above. Elixir’s v1.20.2 Kernel.SpecialForms reference also distinguishes local and remote captures when hot code is reloaded: local captures dispatch to the version present when created, while remote captures dispatch to the current version. That distinction concerns captured named functions, not how the anonymous self-argument example recurses.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →When named recursive functions are clearer
If the operation belongs in a module, named function clauses often make its stopping case and recursive progress easier to see, and provide a stable name for reuse. For example, the list-length pattern shown by Elixir School’s Functions lesson matches an empty list as the base case and a nonempty list as the recursive case:
Rank #3
def length([]), do: 0
def length([_head | tail]), do: 1 + length(tail)
The empty-list clause returns zero; the other clause removes one item by recurring on the tail. For production code, choose the form that makes the base case, progress toward termination, and intended reuse clear. An anonymous function is useful when the function should remain a value in the surrounding expression; a named function is generally easier to follow when it is a reusable module operation.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




