BEAM Erlang Campus

Erlang lesson

Erlang recursion explained in a way that sticks

Recursion is not a trick in Erlang. It is one of the normal ways data gets processed. If you understand the base case, the shrinking step, and the next call, recursion becomes predictable instead of scary.

Why recursion is normal in Erlang

Erlang avoids the classic loop-heavy style many beginners expect. Instead, lists and other structures are often processed by repeatedly taking one piece, solving a smaller version of the problem, and stopping at a simple end case. That is recursion in its practical form.

This matters because the language is designed around immutable data. Since you do not update a shared counter inside a loop the same way you might in other languages, recursion becomes a natural control structure rather than an advanced detour.

The three-part mental model

A good recursive function usually has three parts: the base case, the recursive case, and the shrinking rule. The base case handles the simplest input such as []. The recursive case handles a larger shape such as [H | T]. The shrinking rule explains why the next call is closer to the base case.

Beginners improve quickly when they force themselves to name those three parts aloud before coding. If any one of them is missing, the function will usually stay confusing.

sum([]) -> 0;
sum([H | T]) -> H + sum(T).

How to read recursive code without panic

Start by looking for the stop condition. Then find the expression that makes the problem smaller. Only after that should you worry about the returned value. Many learners do the reverse and get lost in the arithmetic or list operations too early.

A useful habit is tracing one tiny example by hand. For sum([2,3]), ask what the first call returns, what the next call sees, and when [] is reached. Recursion becomes much easier when you draw the input shrinking step by step.

Common recursion mistakes

The biggest mistake is forgetting a true base case. Another is making a recursive call that does not move closer to the base case. Learners also sometimes try to do too much in one clause instead of writing a clean rule plus a simple next call.

The fix is to simplify the data path. Write the smallest valid base case first, then add one recursive clause that clearly reduces the input. If you cannot explain why the next input is smaller, the function is not ready yet.

Simple recursion and tail recursion

Searchers looking for erlang recursion often want more than a beginner definition. They eventually need to understand why some recursive functions are written in a direct style and others use an accumulator. That deeper topic does not need to overwhelm the first lesson, but it should be introduced early enough that the learner knows recursion keeps growing in useful directions.

The important beginner takeaway is that both styles still depend on the same core idea: a clear base case and a smaller next step. Once that is stable, tail recursion becomes an optimization concept instead of a source of confusion.

What to practice after recursion

  • Trace one recursive function on paper.
  • Write a base case for [] before writing the recursive step.
  • Explain why the input gets smaller on every call.
  • Then move on to processes and message passing.

Frequently asked questions

Is recursion required in Erlang?

You can use other tools in some places, but recursion is a fundamental and very common way to process data and keep processes running.

How do I get better at recursion in Erlang?

Practice small list functions, always identify the base case first, and trace one concrete example by hand.