Write while loop equivalent in elixir

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