Underwater, of course, both the for-construct as enumerables use recursion themselves, and are indeed defined similar to your @Onor.io´s program above .
What might be also interesting, is how to emulate a while-loop in Elixir (but this too something that you should not use/need in practice – there are better constructs providing similar functionality):
defmodule While do
@doc """
Loops `body` until a value is thrown using `throw/1`
Passed to `body` is the `starting_value` (which defaults to `nil`) in the first iteration
in the next iterations, the result of the previous iteration is used as passed parameter.
"""
def loop(body, starting_value \\ nil) when is_function(body) do
try do
iteration_result = body.(starting_value)
loop(body, iteration_result)
catch
thrown_result -> thrown_result
end
end
def random_example do
result = loop fn ->
x = :rand.normal
IO.inspect x
if x > 1,do: throw x
end
IO.puts "Result: #{result}"
end
def counter_example do
loop(fn x ->
if x > 100, do: throw "DONE"
IO.puts x
x + 1
end, 0)
end
end






















