ThoughtStorms Wiki

Quora Answer : Computer Science: Why is recursion more elegant than iteration?

Jan 15, 2015

Recursion is more general.

An iterative solution is fine for data which is linear like arrays and linked-lists; or processes which are linear, like "do this, 10 times".

But iteration gets increasingly messy when you want to traverse trees or other complex structures of indefinite depths. Or you want to do things that may branch between different options in different cases. Or trampoline between two complementary processes.

As the complexity of your data / process increases, recursive approaches tend to keep track, scaling more or less linearly with the problem. Iterative approaches can spiral into their own combinatorial complexities as you keep trying to write extra code to flatten the complex data / task back into one-dimensional sequences so that it can be iterated over.

Quora Answer : Are there any useful recursive functions that do not contain a base case?

May 7, 2011

When doing parallel programming in Erlang people will often set off a little server process running in an infinite loop. Because Erlang uses tail-recursion for infinite loops they look something like this :

loop() ->
  receive {message1} -> 
  receive{message2} -> 
  Other -&gt;

The recursion will only end when the process dies.

No Backlinks