# How To Do A For Loop In Elixir (using only recursion)

**URL:** <https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595>\
**Category:** Discussions\
**Tags:** recursion, code-snippets\
**Created:** [May 13, 2016, 1:18pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595 "2016-05-13T13:18:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Onor.io](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/onor.io/32/960_2.png) [@Onor.io](https://forum.elixirforum.com/u/Onor.io)\
**Post date:** [May 13, 2016, 1:18pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/1 "2016-05-13T13:18:56Z")

</div>

One question that came up for me when I first started with FP was how to do things that I was able to do in imperative without mutable variables. One that was tricky was trying to figure out how to do a _for_ loop with no mutable variables. There are better ways to do this (which I am sure others will share) but this is what I’d consider the simplest way to do a for loop in Elixir via recursion:

```
# How to do a "for" loop in Elixir via recursion

defmodule ForLoop do

  def for_loop(count, action) when is_integer(count) and is_function(action) do
    acc = 0
    loop(action, count, acc)
  end

  defp loop(action, count, acc) do
    if acc <= count do
      action.(acc)
      loop(action, count, acc+1)
    end
  end

end

#Simply write out the numbers 0-10
ForLoop.for_loop(10,&(IO.puts(&1)))

```

---

<div class="post-metadata">

**Author:** ![thousandsofthem](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thousandsofthem/32/1030_2.png) [@thousandsofthem](https://forum.elixirforum.com/u/thousandsofthem)\
**Post date:** [May 13, 2016, 1:34pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/2 "2016-05-13T13:34:51Z")

</div>

Simplest way i know:

`for x <- 0..10, do: IO.puts x`

---

<div class="post-metadata">

**Author:** ![jwarlander](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jwarlander/32/1155_2.png) [@jwarlander](https://forum.elixirforum.com/u/jwarlander)\
**Post date:** [May 13, 2016, 1:47pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/3 "2016-05-13T13:47:55Z")

</div>

If you wanted to implement a for loop as a re-usable concept, for the sake of understanding Elixir, recursion, pattern matching etc, then sure 🙂 I might do things _very_ slightly differently;

```elixir
defmodule ForLoop do

  def for_loop(count, action) when is_integer(count) and is_function(action) do
    loop(action, count, 0)
  end

  defp loop(_action, count, acc) when acc > count, do: :ok
  defp loop(action, count, acc) when acc <= count do
      action.(acc)
      loop(action, count, acc+1)
  end

end

```

However, in real life I’d just do as @thousandsofthem suggests, or use `Enum.each`:

```elixir
Enum.each(0..10, &(IO.puts(&1)))

```

---

<div class="post-metadata">

**Author:** ![sztosz](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sztosz/32/1251_2.png) [@sztosz](https://forum.elixirforum.com/u/sztosz)\
**Post date:** [May 13, 2016, 4:21pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/5 "2016-05-13T16:21:38Z")

</div>

That blog entry is irrelevant 😉 Do you really consider your solution to be simplest?

`ForLoop.for_loop(10,&(IO.puts(&1)))`  
`Enum.each(0..10, &(IO.puts(&1)))`  
`for x <- 0..10, do: IO.puts x`

Try to look at this code like had no knowledge about programming whatsoever, and answer yourself this: Which line describes what is going to happen when you run it best?

There was some passive aggressive PS. here. And it was uncalled for.

---

<div class="post-metadata">

**Author:** ![Onor.io](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/onor.io/32/960_2.png) [@Onor.io](https://forum.elixirforum.com/u/Onor.io)\
**Post date:** [May 13, 2016, 5:38pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/6 "2016-05-13T17:38:05Z")

</div>

@sztosz This is what I said:

> [@Onor.io](#):
>
> the simplest way to do a for loop in Elixir via recursion

Take extra note of the part about “via recursion”. Of course there are simpler ways to write that code. I was trying to demonstrate how to write the code with recursion specifically for developers that are new to FP.

Yes, we can tell them to use a _for_ comprehension or an Enum.each. But the point of my post was to share a technique to write that particular construct using _recursion_.

---

<div class="post-metadata">

**Author:** ![sztosz](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sztosz/32/1251_2.png) [@sztosz](https://forum.elixirforum.com/u/sztosz)\
**Post date:** [May 13, 2016, 6:10pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/7 "2016-05-13T18:10:23Z")

</div>

Ok, but what is the gain here for programmers new to elixir or even to programming? Recursion is great, but why use it for using for\_loop, especially when in your example it is unclear what argument is sent to the action in for\_loop call, and also it does not allow you to pass any argument to your action. I think there are many good examples of recursion, and your unfortunately is just an example of over-complicating code.

---

<div class="post-metadata">

**Author:** ![Onor.io](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/onor.io/32/960_2.png) [@Onor.io](https://forum.elixirforum.com/u/Onor.io)\
**Post date:** [May 13, 2016, 7:13pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/8 "2016-05-13T19:13:44Z")

</div>

Please share a good example of recursion so I can see how best to go back and edit my example to make it clearer.

---

<div class="post-metadata">

**Author:** ![Qqwy](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/32/1349_2.png) [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Post date:** [May 13, 2016, 7:20pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/9 "2016-05-13T19:20:32Z")

</div>

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_):

```elixir
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

```

---

<div class="post-metadata">

**Author:** ![sztosz](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sztosz/32/1251_2.png) [@sztosz](https://forum.elixirforum.com/u/sztosz)\
**Post date:** [May 13, 2016, 7:40pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/10 "2016-05-13T19:40:18Z")

</div>

```elixir
defmodule MyList do
  def flatten([]), do: []

  def flatten([head | tail]) do 
    flatten(head) ++ flatten(tail)
  end

  def flatten(head), do: [head]
end

IO.inspect MyList.flatten([[1], [2, [3] ] , [4]]) # Returns [1,2,3,4]
IO.inspect MyList.flatten([[], [[], [3] ] , [4]]) # Returns [3,4]

```

Taken from [Benjamin Tan's Learnings & Writings - Elixir for the Lazy, Impatient and Busy: Part 1 |\> Lists and Recursion](http://benjamintan.io/blog/2013/06/13/elixir-for-the-lazy-impatient-and-busy-lists-and-recursion/)

```elixir
defmodule Factorial do  
  def of(0), do: 1
  def of(n), do: of(n, 1)
  def of(1, acc), do: acc 
  def of(n, acc) when n > 1 do: of(n - 1, acc * n)
end

```

Taken from [Comparing Elixir and Go](https://blog.codeship.com/statefulness-in-elixir/)

Those are good examples of recursion. Re-implementing basic loops it not, especially if that loop is not a general purpose loop, and can only be used if only argument for function that loop calls is the inner loop-accumulator. When someone new to programming or Elixir will see that for\_loop, he can be tempted to use it, and be confused when simple loop will start to throw errors.

---

<div class="post-metadata">

**Author:** ![Onor.io](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/onor.io/32/960_2.png) [@Onor.io](https://forum.elixirforum.com/u/Onor.io)\
**Post date:** [May 13, 2016, 8:13pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/11 "2016-05-13T20:13:31Z")

</div>

No question there are better approaches. I had a novice ask me at a meetup about how to code something with recursion and I was able to answer him but I told him that I almost never use recursion in practice because there are better approaches (like **Enum.each** and the **for comprehension** ). I was simply trying to provide a small, trivial example of how to emulate a _for_ loop from an imperative language in Elixir.

---

<div class="post-metadata">

**Author:** ![Onor.io](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/onor.io/32/960_2.png) [@Onor.io](https://forum.elixirforum.com/u/Onor.io)\
**Post date:** [May 13, 2016, 8:17pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/12 "2016-05-13T20:17:32Z")

</div>

Those are both excellent examples of recursion but considering that I was trying to demonstrate how one could simulate a _for_ loop from an imperative language (something analogous to

> for(i=0; i \<=10; i++)  
> {  
> action();  
> }

those would not have been the examples I would have picked. I said right from the start that I was solely trying to demonstrate how to do something analogous to an imperative for loop without mutable values.

---

<div class="post-metadata">

**Author:** ![sztosz](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sztosz/32/1251_2.png) [@sztosz](https://forum.elixirforum.com/u/sztosz)\
**Post date:** [May 13, 2016, 8:57pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/13 "2016-05-13T20:57:25Z")

</div>

Ok i think the issue is that you should not do that at all. You should not simulate imperative or object oriented (with mutable state) constructs in functional languages. Your example is still not good.  
`for x <- 0..10, do: action(x)`  
is what you probably should have used.

Anyway that old plain C-like for loop with starting value accumulator is something that is not really readable, and has no use in modern languages.  
In ruby you have:

```elixir
some_objects.each { |an_object| action(object) }
# or
for an_object in some_objects
  action(an_object)  
end

```

In python you also iterate over collection like this

```elixir
for an_object in some_objects:
    action(an_object)

```

Even in Java since Java 5 you would probably do

```elixir
for(ClassName an_object : some_objects) {
   action(an_object)
}

```

One of my most hated languages: JavaScript, has similar constructs

```elixir
function callback_function(currentObject, index, array) {
  action(currentObject)
}
some_objects.forEach(callback_function)

```

Why? Because all of those are much more readable and descriptive, then that old plain C-like for loop. Answer yourself, how often you really need that index? Don’t you really want the object that stands behind that index?

And in Elixir you have what @thousandsofthem and @jwarlander wrote. And you should be using it instead of trying to wrap some logic into totally (in Elixir) out of place C-like for loop 🙂

---

<div class="post-metadata">

**Author:** ![gregvaughn](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gregvaughn/32/954_2.png) [@gregvaughn](https://forum.elixirforum.com/u/gregvaughn)\
**Post date:** [May 13, 2016, 10:07pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/14 "2016-05-13T22:07:20Z")

</div>

I’m jumping in late here, but I worry this thread is going off in the weeds. If one is helping to introduce an imperative programmer into Elixir and they ask “how do you handle for loops?” is it more helpful to show them that recursion can do everything imperative iteration can do, or jump immediately to higher order functions or for comprehensions? I see value in showing the recursive solution, but quickly moving over it into better abstractions. IMHO, if a functional programmer has not, at some point, implemented map and reduce with recursion, then they don’t really understand FP.

---

<div class="post-metadata">

**Author:** ![sashaafm](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sashaafm/32/2636_2.png) [@sashaafm](https://forum.elixirforum.com/u/sashaafm)\
**Post date:** [May 13, 2016, 10:21pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/15 "2016-05-13T22:21:02Z")

</div>

I’ve never implemented map and reduce and I don’t feel like I’m a worse functional developer because of that? (maybe I did implement them in Haskell classes a few years ago). Why do you say that about map and reduce specifically?

---

<div class="post-metadata">

**Author:** ![gregvaughn](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gregvaughn/32/954_2.png) [@gregvaughn](https://forum.elixirforum.com/u/gregvaughn)\
**Post date:** [May 13, 2016, 10:58pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/16 "2016-05-13T22:58:15Z")

</div>

Maybe I spoke too hastily. map can be implemented on top of reduce. reduce can be implemented on top of recursion. I’m drawing a blank right now for what recursion can do that reduce cannot. I suppose my core point is that reduce is a level above recursion. If you can implement reduce from recursion, then you’ve got a solid handle on the core of FP. Other higher order functions can be derived from reduce.

---

<div class="post-metadata">

**Author:** ![bentanweihao](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bentanweihao/32/326_2.png) [@bentanweihao](https://forum.elixirforum.com/u/bentanweihao)\
**Post date:** [May 14, 2016, 12:50am UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/17 "2016-05-14T00:50:00Z")

</div>

All these talk about `fold` reminds me of a paper that I’ve yet to read: [A tutorial on the universality and  
expressiveness of fold](http://www.cs.nott.ac.uk/~pszgmh/fold.pdf)

---

<div class="post-metadata">

**Author:** ![Qqwy](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/32/1349_2.png) [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Post date:** [May 14, 2016, 7:36am UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/18 "2016-05-14T07:36:43Z")

</div>

> [@bentanweihao](#):
>
> All these talk about `fold` reminds me of a paper that I’ve yet to read: [A tutorial on the universality and  
> expressiveness of fold](http://www.cs.nott.ac.uk/~pszgmh/fold.pdf)

Hm! Interesting paper. Very cool to see that so much can be built on top of `foldr` / `foldl`.

One thing the paper does not state, (and is often overlooked), is that `foldl` is tail-recursive, while `foldr` is not. There is [a great example on the Haskell wiki](https://wiki.haskell.org/Foldr_Foldl_Foldl') about why this is the case.

* * *

**EDIT:** It seems that the distinction between tail-call and non-tail-call functions in Erlang/Elixir is not that important.

See:

- [Erlangs Tail Recursion is not a silver bullet](http://ferd.ca/erlang-s-tail-recursion-is-not-a-silver-bullet.html)
- [the Eight Myths of Erlang Performance](http://erlang.org/doc/efficiency_guide/myths.html#id60476)
- and also [this rejected Pull-Request by me to add tail-recursion to the documentation of foldr/foldl 😅](https://github.com/elixir-lang/elixir/pull/4623)

---

<div class="post-metadata">

**Author:** ![mkunikow](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mkunikow/32/235_2.png) [@mkunikow](https://forum.elixirforum.com/u/mkunikow)\
**Post date:** [May 14, 2016, 12:04pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/19 "2016-05-14T12:04:43Z")

</div>

There is nice book [Functional Programming in Scala - Paul Chiusano and Runar Bjarnason](https://www.manning.com/books/functional-programming-in-scala) .There a a lot common concepts and more like what is pure function . The drawback is the code is in Scala 🙂

---

<div class="post-metadata">

**Author:** ![rvirding](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rvirding/32/1409_2.png) [@rvirding](https://forum.elixirforum.com/u/rvirding)\
**Post date:** [May 14, 2016, 12:05pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/20 "2016-05-14T12:05:06Z")

</div>

I also think there is one benefit for basically everyone of implementing various types of loops explicitly using recursion, and that is to understand what is really going on. So while you might most of the time use libraries or provided constructs instead of actually explicitly doing yourself, really knowing what is going will make you a better programmer. It will allow you use libraries and provided constructs in a better way.

Robert

---

<div class="post-metadata">

**Author:** ![sztosz](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sztosz/32/1251_2.png) [@sztosz](https://forum.elixirforum.com/u/sztosz)\
**Post date:** [May 14, 2016, 2:11pm UTC](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595/21 "2016-05-14T14:11:44Z")

</div>

Wise words from Programming Elixir by Dave Thomas

> L. Peter Deutsch once penned, “To iterate is human, to recurse divine.” And  
> that’s certainly the way I felt when I first started coding Elixir. The joy of  
> pattern-matching lists in sets of recursive functions drove my designs. After  
> a while, I realized that perhaps I was taking this too far.  
> In reality, most of our day-to-day work is better handled using the various  
> enumerators built into Elixir. They make your code smaller, easier to understand,  
> and probably more efficient.  
> Part of the process of learning to be effective in Elixir is working out for  
> yourself when to use recursion and when to use enumerators. I recommend  
> enumerating when you can.

[Next page](https://forum.elixirforum.com/t/how-to-do-a-for-loop-in-elixir-using-only-recursion/595.md?page=2)
