Write while loop equivalent in elixir

I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.

total = 10
while total != 0
   puts "hello"
   total -= 1
end

The question and answer in this example use list for loop. Convert ruby while loop into elixir

2 Likes

I found Stream.unfold to be quite useful for places where I don’t know how many iterations are needed to get to the end.

stream = Stream.unfold(10, fn x -> 
  if x != 0 do
    IO.puts "Hello"
    {:ok, x - 1}
  else
    nil
  end 
end)

Stream.run(stream)

There’s also Enum.reduce_while, but it needs an enumerable as input to start with.

9 Likes

Something like this?

def while(side_effect, i, min) when i == min do
  side_effect
end

def while(side_effect, i, min) do
  side_effect
  while(side_effect, i - 1, min)
end
while((puts "hello"), 10, 0)
1 Like

I think recursion is the simplest way to go…

defmodule Looper do
  def say_hello(times_left) do
    case times_left do
      0 ->
        :ok

      x ->
        IO.puts("hello")
        say_hello(x - 1)
    end
  end
end

Looper.say_hello(10)

You could use if instead of case since there are only two patterns.

Alternatively:

defmodule Looper do
  def say_hello(times_left) when times_left > 0 do
    IO.puts("hello")
    say_hello(times_left - 1)
  end

  def say_hello(times_left) when times_left == 0 do
    :ok  
  end
end

Looper.say_hello(10)
3 Likes
defmodule Demo do

  def while(pred, next, data) do
    case pred.(data) do
      true ->
        while(pred, next, next.(data))
      _ ->
        data
    end
  end

  def positive_non_zero?(i),
    do: i > 0

  def say_hello_once(i) do
    IO.puts("hello")
    i - 1
  end

  def demo1,
    do: while(&positive_non_zero?/1, &say_hello_once/1, 10)

  def demo2 do
    data = {1.01, [1.01, 1.02, 1.04]}

    p = fn {i,p} ->
      i in p
    end

    n = fn {last, p} ->
      i = last + 0.01
      IO.puts(i)
      {i, p}
    end

    while(p, n, data)
  end

end

IO.puts("# demo1")
IO.inspect(Demo.demo1())
IO.puts("# demo2")
IO.inspect(Demo.demo2())
$ elixir demo.exs
# demo1
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
0
# demo2
1.02
1.03
{1.03, [1.01, 1.02, 1.04]}
$

Refactoring: Replace Iteration with Recursion
Refactoring: Replace Recursion with Iteration

Recursion to Iteration Series
Recursion? We don’t need no stinking recursion!

2 Likes

Try this

   def while(1) do 
     puts "hello"
   end

   def while(x) do
     puts "hello"
     while(x-1)
   end

I just want to comment on something, at first glance all the solutions posted on this page look kind of complicated for such a simple thing. But you almost never need to write such code in the real world.

In actual apps, you want to iterate over some collection of data items. The for or while loop is how you do that in an imperative language: you have some “iterator” kind of value–a simple index for an array, something else for a map or set–increment that, check if it’s out of range, use it to access your data collection.

In functional languages, you pass the operation you want to perform on the data as an argument to one of the collection’s functions. In Elixir, Enum.map or Enum.reduce. This actually results in more compact code because it eliminates the whole “get an iterator, increment, check it, use it” dance.

So while this particular exercise can help you understand some low-level mechanics of Elixir, it could also be misleading. Please don’t think you have to jump through the hoops of all these solutions in order to do something with an array of values you get back from your database or submitted from a web form :wink:

That said, here is my solution:

Enum.each(10..1, fn(i) -> IO.puts(i) end)

or, using some shorthand:

Enum.each(10..1, &(IO.puts("#{&1}")))

or, if you want to prove you actually understand passing around functions:

Enum.each(10..1, &IO.puts/1)

But even that could be misleading, because in a functional language you don’t often just “run a loop” over a collection for side effects, you usually produce a value, either a set of values produce from each source value (Enum.map) or a single value derived from the whole set (Enum.reduce).

13 Likes

Almost, but not quite :wink:

  def while(0) do
  end

  def while(x) do
     puts "hello"
     while(x - 1)
  end

And in real life, you probably want def while(x) when x > 0 do

@Zesky665 Haha, you & I were correcting at the same time!

1 Like

However by bypassing the “low-level mechanics” (and going straight for higher order functions) you are giving up on the opportunity to explore the general connection (and differences) between recursion and iteration, for example:

  • how iteration fundamentally relies on mutability to operate
  • while recursion accomplishes the the same job in an immutable environment[**]

Granted that lesson seems more important in an environment where mutability is a possible choice - where it might be prudent to be “Immutable where possible, mutable (only) when needed”.

[**] which is how the basic HOFs operate.

look kind of complicated for such a simple thing.

Another matter is that a while loop isn’t just one single concept. There is the idea of

  • the body that is executed
  • the predicate which determines whether the body is executed (again)

So the concept of a while loop may actually be seen as more basic than it actually is just because certain programming languages offer a single statement or expression as a representation for it.

2 Likes

You’re right, I always forget that you can just leave a function empty and it won’t cause an error.

Correct me if I’m wrong (It’s been a few years since my last comp sci course) but aren’t while loops in most languages just synaptic sugar for recursive functions. By which I mean that they compile down to the same code and aren’t functionally much different.

Specifically it returns the atom nil.

Depends on the back-end, if the back-end is, say LLVM, then it compiles to something that looks recursive but it bases it on blocks instead of function calls, similar concept though.

Going down to assembly language a loop often has a test, conditional jump, body and unconditional jump - which really is iterative.

Also keep in mind that iteration doesn’t require a stack frame and only tail call (last call optimized) recursion can reuse a stack frame while body recursion consumes a stack frame on each call.

1 Like

LLVM has that as well except you can only reference bindings defined before, and bindings are immutable, so you have to allocate memory somewhere (stack is common) to have mutable memory. :wink:

Regardless of what it compiles down to, iteration is a special case, recursion is more general.

The major difference (from the developer’s point of view) between a while loop and a recursive function is that in every other language a recursive function allocates a new stack frame and therefore has a limit to how many times it can be recursively executed. In Elixir this is not true because a stack frame is not allocated due to tail recursion. That is why a while loop is not necessary in Elixir.

In some other languages–you might be surprised how few. For starters, basically all functional languages have tail call optimization (without explicit looping constructs built in they have to), and that includes the JVM-based ones like Scala & Closure. But also C, C++, C# (actually all .NET languages because the JIT does it), Objective-C, Javascript, Kotlin…

The JVM itself doesn’t TCO - so it’s up to the language to fake it.

Clojure in particular has recur to support iteration - it essentially replaces the data in a “recursion frame” before reentering the body of a loop; it’s an iterative loop based on the concept of tail recursive calls (probably one of the best examples of how iteration and recursion are two sides of the same coin).

1 Like

Yep, that’s why Java wasn’t on my list :wink: