Is recursion truly superior to iteration?

  • Thread starter Thread starter dashkin111
  • Start date Start date
  • Tags Tags
    Recursion
Click For Summary

Discussion Overview

The discussion revolves around the comparative advantages of recursion versus iteration in programming. Participants explore various scenarios and algorithms where recursion may be more beneficial or simpler to implement than iteration, as well as instances where iteration might be preferred.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • Some participants question when recursion is truly better than iteration, expressing difficulty in identifying such cases.
  • Others argue that divide-and-conquer algorithms, such as quicksort and the fast Fourier transform, are easier to implement recursively than iteratively.
  • A participant shares their experience with the Ackermann function, noting that implementing it in a non-recursive language is challenging compared to a recursive approach.
  • One participant suggests that certain programming tasks, like expression evaluation and directory tree traversal, naturally lend themselves to recursive solutions.
  • Another participant highlights that tree traversal can be particularly cumbersome in non-recursive languages, requiring additional machinery to achieve similar results.
  • Enumerating all subsets and permutations of a set is also mentioned as a task that benefits from recursion.

Areas of Agreement / Disagreement

Participants express differing views on the superiority of recursion versus iteration, with some advocating for recursion in specific contexts while others remain skeptical about its advantages. No consensus is reached regarding a definitive preference for one approach over the other.

Contextual Notes

Some participants note that certain examples commonly used to illustrate recursion, such as the factorial function, may not favor recursion over iteration, indicating that the effectiveness of each approach can depend on the specific problem being addressed.

Who May Find This Useful

This discussion may be useful for programmers, computer science students, and individuals interested in algorithm design and implementation strategies.

dashkin111
Messages
47
Reaction score
0
When is recursion really better than iteration. I'm not a programmer by major, but I can't think of any time when recursion would be better
 
Technology news on Phys.org
There are many divide-and-conquer algorithms that are much easier to implement by recursion than by iteration. Examples that come to mind are quicksort, the fast Fourier transform, and the fast multipole method. Unfortunately, the most common example used to illustrate recursion is the factorial function, which is better implemented by iteration.
 
Try writing a program to compute the Ackermann function in a non-recursive language. I still remember that exercise twenty some years. Doing this in Fortran and assembly was painful. In a recursive language it takes but a few lines of code. The Ackermann function is

Edit: I give up. Sometimes the limited LaTeX here bites.
A(m,n) = n+1 if m=0,
A(m,n) = A(m-1,1) if n=0,
A(m,n) = A(m-1,A(m,n-1)) otherwise.
 
Last edited:
There are cases where the nature of the programming is recursive. One example is an expression evaluator that allows parenthesis (like a calculator). It's easier to use just make a recursive call for each "(" encountered, and to return with a value for each ")" encountered in such a case, but otherwise just iterate for all the mathematical operators like + - x / (assuming no precedence here).

Another example is a program that follows a directory tree, especially if the program processes all files within a directory before processing sub-directories. I think of this as a program that makes uses recursion to create a "new instance" of a program state, with the ability to return to a "previous instance".

I use the following logic as the basis of a file copy program to backup / restore / defrag partitions on my system. The issue in this sample code is that there are two loops and the second loop needs to be able to "call" a new instance of both loops, then "return" back to a previous instance of the second loop.

Code:
ProcessDirectory(...)
{

    ForEachFile()
    {
        ProcessFile();
    }

    ForEachDirectotry()
    {
        ProcessDirectory()
    }
}

ProcessFile(...)
{

}
 
Last edited:
Excellent example, Jeff. Tree traversal is a bear in a non-recursive language. You essentially have to reinvent all of the machinery you get for free with recursion to do tree traversal iteratively.
 
Also for enumerating all subsets and permutations of an input set of n elements for example.
 
Thanks everyone, this is helpful to me.
 

Similar threads

  • · Replies 62 ·
3
Replies
62
Views
14K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K