Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should recursion be preferred over iteration?

People also ask

Where is recursion preferred over iteration?

If time complexity is the point of focus, and number of recursive calls would be large, it is better to use iteration. However, if time complexity is not an issue and shortness of code is, recursion would be the way to go.

When should you choose a recursive algorithm over an iterative algorithm?

When should you choose a recursive algorithm over an iterative algorithm? In situations where the recursive algorithm is easier to design. Specifically, situations where a problem can be broken down into small repetitions of very similar problems.

Are recursive methods always better than iterative methods?

It depends on the problem you are trying to solve. In general, if your algorithm has to solve all the sub problems of the original problem, then an iterative algorithm is better, because recursive algorithms have an overhead for procedure calls and iterative algorithms don't have that overhead.

Is recursion always better than iteration in C?

Recursion has a large amount of overhead as compared to Iteration. It is usually much slower because all function calls must be stored in a stack to allow the return back to the caller functions. Iteration does not involve any such overhead.


Iteration is more performant than recursion, right?

Not necessarily. This conception comes from many C-like languages, where calling a function, recursive or not, had a large overhead and created a new stackframe for every call.

For many languages this is not the case, and recursion is equally or more performant than an iterative version. These days, even some C compilers rewrite some recursive constructs to an iterative version, or reuse the stack frame for a tail recursive call.


Try implementing depth-first search recursively and iteratively and tell me which one gave you an easier time of it. Or merge sort. For a lot of problems it comes down to explicitly maintaining your own stack vs. leaving your data on the function stack.

I can't speak to Haskell as I've never used it, but this is to address the more general part of the question posed in your title.


Haskell do not allow iteration because iteration involves mutable state (the index).


As others have stated, there's nothing intrinsically less performant about recursion. There are some languages where it will be slower, but it's not a universal rule.

That being said, to me recursion is a tool, to be used when it makes sense. There are some algorithms that are better represented as recursion (just as some are better via iteration).

Case in point:

fib 0 = 0
fib 1 = 1
fib n = fib(n-1) + fib(n-2)

I can't imagine an iterative solution that could possibly make the intent clearer than that.


Here is some information on the pros & cons of recursion & iteration in c:

http://www.stanford.edu/~blp/writings/clc/recursion-vs-iteration.html

Mostly for me Recursion is sometimes easier to understand than iteration.


Iteration is just a special form of recursion.