Recursion in Programming and When to Use or Not to Use It

  • Context: Insights 
  • Thread starter Thread starter bhobba
  • Start date Start date
  • Tags Tags
    Programming Recursion
Click For Summary

Discussion Overview

The discussion revolves around the concept of recursion in programming, exploring its applications, complexities, and challenges. Participants share their experiences with recursion across various programming languages, including FORTRAN, Pascal, Java, and functional languages, while also touching on educational perspectives and practical examples.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants note that recursion can simplify complex problems but also caution about its potential pitfalls, particularly in team environments.
  • One participant reflects on their initial struggles with understanding recursion and how it became clearer over time, particularly through the lens of induction.
  • Another participant emphasizes the importance of having a well-defined base case to prevent StackOverflow exceptions in recursive functions.
  • There is a mention of tail call elimination in functional languages, which can mitigate stack overflow issues, though it only applies to certain types of recursive functions.
  • A participant provides an example of using recursion in an expression parser, illustrating how recursion can evaluate nested expressions.
  • Some participants discuss the historical context of programming languages and their influence on understanding recursion, with references to LISP and its foundational principles.

Areas of Agreement / Disagreement

Participants express a range of views on the effectiveness and challenges of recursion, with no clear consensus on its advantages or best practices. The discussion includes both supportive and critical perspectives on recursion's use in programming.

Contextual Notes

Some limitations are noted regarding the understanding of recursion, including the dependence on language-specific features and the potential for ill-defined base cases leading to errors.

Who May Find This Useful

This discussion may be of interest to programmers, computer science students, and educators exploring the complexities of recursion in various programming languages.

  • #61
PeterDonis said:
so many algorithms can be implemented as loops instead of recursively
TL;DR ANY algorithm can be implemented with either loops or recursive function calls, so (unless and until you identify that the implementation is too expensive in time or memory) use whichever you find easier to code and therefore more reliable and easier to maintain.

Details
  • ANY algorithm can be implemented with loops instead of recursive function calls, however some are easier to implement recursively (e.g. quicksort). Which runs faster and/or with less memory depends on a number of factors.
  • ANY algorithm can be implemented with recursive function calls instead of loops, however some are easier to implement with loops (e.g. Fibonacci numbers). Algorithms that are easier to implement with loops run faster and/or with less memory unless the recursion can be performed with exclusively tail calls and the compiler is designed (and set) to optimize very aggressively.
  • SOME algorithms cannot be implemented with exclusively tail call recursion. These are often easier to implement recursively because iterative implementation requires the programmer to maintain one or more stacks: again quicksort is a good example, and again which (if either) runs faster and/or with less memory depends on a number of factors.
 
  • Like
Likes   Reactions: Mark44
Technology news on Phys.org
  • #62
pbuk said:
ANY algorithm can be implemented with either loops or recursive function calls
Yes, I was being imprecise; by "loop" I meant a loop that does not have to add more information to some data structure on every iteration; it can update information on every iteration, but only if it throws away the old information so the total information kept track of is of (roughly) constant size.

An example in Python would be a for loop using the range built-in iterator; this does not compute all of the numbers to be iterated over in advance, it only computes them one at a time as they are needed, and throws away the old one each time it computes a new one.

You could of course implement something similar using recursion, as long as whatever language you were using could optimize away the parts that would require storing more data on each iteration (for example, tail call optimization to eliminate repeated pushing of parameters on the stack).
 
  • #63
pbuk said:
TANY algorithm can be implemented with loops instead of recursive function calls, however some are easier to implement recursively (e.g. quicksort). Which runs faster and/or with less memory depends on a number of factors.

Yes. But it's the whole structured programming thing we had constantly drummed into us during my university days. The choice is made on which will be the most maintainable. Deciding that of course is both art and science. The last time I posted about it on another forum frequented by more modern programmers than me the response I got was - Give me that old-time religion :DD:DD:DD:DD:DD.

Thanks
Bill
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
15K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K