HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ElixirVariablesData TypesOperatorsStringsListsConditions (if / else)Iteration (Enum)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Enum
🔀 Condition
🧩 Function
Elixir›Iteration (Enum)

Iteration (Enum)

There are no loops in Elixir; iteration is done using Enum.each, Enum.map, and Enum.sum.

Simple enough for a 5-year-old to understand

Iterate with Enum.each

Instead of for/while, Enum.each is used. 1..3 is a range (inclusive of 3).

Example code
1
Enum.each(1..3, fn i ->
The function runs for each element.
LoopThe function runs for each element.
2
IO.puts(i)
1, 2, 3
Output / Input1, 2, 3
3
end)
LoopOutput / Input
💡 Hint: The range 1..3 includes 3 as well.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the numbers from 1 to 5 one below another using Enum.each.

🎯 Expected output: 1 2 3 4 5

Loading editor…

About this topic

There are no loops in Elixir. Because data is immutable, a classic loop by incrementing a counter cannot be built. Iteration is done in three ways: Enum functions, list comprehensions, and recursion.

In everyday code, the Enum module is used most frequently. There are ready-made functions to transform each element, select those matching a condition, and reduce all of them down to a single value. When chained with the pipe operator, the transformation pipeline becomes readable.

Recursion is the most fundamental tool and is used together with pattern matching. You process one element at a time until the list becomes empty. It is necessary for situations where ready-made functions are not enough.

What you will learn

  • Repeating with Enum
  • List comprehension
  • The recursion pattern
  • The reduction operation
  • Lazy streams

Who is this for?

  • Developers looking for loops
  • Those transitioning to functional patterns
  • Those who will process big data

When to use?

Mapping is used if an independent operation is applied to each element, and filtering is used if selection is made based on a condition.

If a single result is produced from a list, reduction is the right tool; sum, count, and grouping are examples of this.

Common mistakes

Wrong

toplam = 0
Enum.each([1, 2, 3], fn x -> toplam = toplam + x end)
IO.puts toplam

The binding inside the block does not leak outside; zero is still printed to the screen and the compiler gives a warning. The way to accumulate in an immutable world is not to update an external variable, but to pass the result of each step to the next — and that is what the reduction function does.

Correct

toplam = Enum.reduce([1, 2, 3], 0, fn x, acc -> acc + x end)
IO.puts toplam

Wrong

sonuc = Enum.map([1, 2, 3], fn x -> IO.puts x end)
IO.inspect sonuc

The mapping function produces a new list from the return value of each element; since printing to the screen returns a nil value, the result becomes a list of nil values. If you only want a side effect, a function dedicated to side effects should be used.

Correct

Enum.each([1, 2, 3], fn x -> IO.puts x end)

Wrong

defmodule Say do
  def say([_ | kuyruk]), do: 1 + say(kuyruk)
end

Because the base case is not defined, when the list runs out there is no function clause left to match and a FunctionClauseError is raised. Every recursive definition must have a stopping point; the empty list case must be written as a separate clause.

Correct

defmodule Say do
  def say([]), do: 0
  def say([_ | kuyruk]), do: 1 + say(kuyruk)
end

Frequently asked questions

How does list comprehension work?

It is written with the for keyword; it combines the source collection, optional filters, and the value to be produced for each element. If you provide multiple sources, it iterates over all combinations. It is not a loop, it produces a new collection.

When is reduction used?

When producing a single result from a collection. You provide an initial value and update the accumulation at each step. Sum, maximum value, count, and grouping are all special cases of reduction.

How is infinite repetition done?

You can generate lazy sequences with the Stream module and take elements until a condition is met. In server processes, a recursive loop function waits for messages and runs forever; this is the standard pattern in Elixir.

Summary

There are no loops; Enum, list comprehensions, and recursion are used.

The binding inside the block does not leak outside.

Accumulation is done with a reduction function.

There is a separate function exclusively for side effects.

Every recursion must have a base case.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code