Questions tagged [recursion]

For questions about recursion, the practice of calling a method or function from within itself.

See here for more information.

165 questions
140
votes
8 answers

Recursion or while loops

I was reading about some development interview practices, specifically about the technical questions and tests asked at interviews and I've stumbled quite a few times over sayings of the genre "Ok you solved the problem with a while loop, now can…
Shivan Dragon
  • 4,583
  • 5
  • 24
  • 31
129
votes
11 answers

Is there anything that can be done with recursion that can't be done with loops?

There are times where using recursion is better than using a loop, and times where using a loop is better than using recursion. Choosing the "right" one can save resources and/or result in fewer lines of code. Are there any cases where a task can…
Pikamander2
  • 1,259
  • 2
  • 9
  • 9
104
votes
5 answers

Why doesn't Java have optimization for tail-recursion at all?

From what I have read: The reason is because it is not easy to determine which method will actually be called as we have inheritance. However, why doesn't Java at least have tail-recursion optimization for static methods and enforce proper way to…
InformedA
  • 2,991
  • 2
  • 19
  • 28
77
votes
18 answers

In plain English, what is recursion?

The idea of recursion is not very common in real world. So, it seems a bit confusing to the novice programmers. Though, I guess, they become used to the concept gradually. So, what can be a nice explanation for them to grasp the idea easily?
Gulshan
  • 9,402
  • 10
  • 58
  • 89
60
votes
3 answers

What's the difference between recursion and corecursion?

What's the difference between these? Recursion Corecursion On Wikipedia, there is little information and no clear code explaining these terms. What are some very simple examples explaining these terms? How is corecursion the dual of recursion?…
user167908
  • 711
  • 1
  • 5
  • 5
49
votes
8 answers

What methods are there to avoid a stack overflow in a recursive algorithm?

Question What are the possible ways to solve a stack overflow caused by an recursive algorithm? Example I'm trying to solve Project Euler problem 14 and decided to try it with a recursive algorithm. However, the program stops with a…
Lernkurve
  • 817
  • 1
  • 7
  • 14
46
votes
11 answers

Is a while loop intrinsically a recursion?

I wondered whether a while loop is intrinsically a recursion? I think it is because a while loop can be seen as a function that calls itself at the end. If it is not recursion, then what is the difference?
badbye
  • 585
  • 1
  • 4
  • 5
46
votes
17 answers

Recursion without factorial, Fibonacci numbers etc

Almost every article I can find about recursion includes the examples of factorial or Fibonacci Numbers, which are: Math Useless in real life Are there some interesting non-math code examples to teach recursion? I'm thinking divide-and-conquer…
synapse
  • 399
  • 3
  • 7
44
votes
8 answers

Are functional languages better at recursion?

TL;DR : Do functional languages handle recursion better than non-functional ones? I am currently reading Code Complete 2. At some point in the book, the author warns us about recursion. He says it should be avoided when possible and that functions…
marco-fiset
  • 8,721
  • 9
  • 35
  • 46
38
votes
2 answers

General way to convert a loop (while/for) to recursion or from a recursion to a loop?

This problem is mainly focusing on the algorithm, maybe something abstract and more academic. The example is offering a thought, I wanna a generic way, so example is only used as to make us more clearly about your thoughts. Generally speaking, a…
xqMogvKW
  • 533
  • 1
  • 5
  • 8
31
votes
5 answers

Performance: recursion vs. iteration in Javascript

I have read recently some articles (e.g. http://dailyjs.com/2012/09/14/functional-programming/) about the functional aspects of Javascript and the relationship between Scheme and Javascript (the latter was influenced by the first, which is a…
mastazi
  • 413
  • 1
  • 5
  • 8
26
votes
9 answers

How do I explain "Recursion" to an 8-year-old kid?

Possible Duplicate: In plain English, what is recursion? What is the best way to explain "Recursion" to 8 years old kid? I tried with the Fibonacci Series but i failed.
Soner Gönül
  • 575
  • 2
  • 9
  • 29
24
votes
7 answers

What imperative programming languages do not support recursion?

To my knowledge, all modern imperative programming languages support recursion in the sense that a procedure can call itself. This was not always the case, but I cannot find any hard facts with a quick Google search. So my question is: Which…
fredoverflow
  • 6,854
  • 8
  • 39
  • 46
23
votes
8 answers

Can I use additional parameters in recursion problems?

Okay, I was being interviewed at a company and the interviewer asked me a recursion problem. It was an online interview, so, he had set up the problem statement and a function signature on CodeSandbox (an online code editor/collaboration tool). I…
Bharat Soni
  • 357
  • 1
  • 2
  • 6
20
votes
10 answers

What is so difficult about pointers/recursion?

In the perils of java schools Joel discusses his experience at Penn and the difficulty of "segmentation faults". He says [segfaults are difficult until you] "take a deep breath and really try to force your mind to work at two different…
P.Brian.Mackey
  • 11,123
  • 8
  • 48
  • 87
1
2 3
10 11