Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Make an Anonymous Function Recurse in Elixir

An Elixir anonymous function can recurse when you pass its function value as an argument and call it with dot syntax. Named clauses are often clearer for reusable recursion.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.(...).

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/1 captures 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.