Questions tagged [recursion]

Questions about objects such as functions, algorithms or data structures that are expressed using "smaller" instances of themselves.

607 questions
76
votes
4 answers

What is tail recursion?

I know the general concept of recursion. I came across the concept of tail recursion while studying the quicksort algorithm. In this video of quick sort algorithm from MIT at 18:30 seconds the professor says that this is a tail recursive…
Geek
  • 1,191
  • 1
  • 10
  • 12
46
votes
5 answers

Iteration can replace Recursion?

I've been seeing all over stack Overflow, e.g here, here, here, here, here and some others I don't care to mention, that "any program that uses recursion can be converted to a program using only iteration". There was even a highly upvoted thread…
Tobi Alafin
  • 1,647
  • 4
  • 17
  • 22
34
votes
8 answers

What is most efficient for GCD?

I know that Euclid’s algorithm is the best algorithm for getting the GCD (great common divisor) of a list of positive integers. But in practice you can code this algorithm in various ways. (In my case, I decided to use Java, but C/C++ may be another…
Jonathan Prieto-Cubides
  • 2,229
  • 3
  • 18
  • 26
26
votes
5 answers

When to use recursion?

When are some (relatively) basic (think first year college level CS student) instances when one would use recursion instead of just a loop?
Taylor Huston
  • 579
  • 1
  • 6
  • 9
23
votes
2 answers

Recursive definitions over an inductive type with nested components

Consider an inductive type which has some recursive occurrences in a nested, but strictly positive location. For example, trees with finite branching with nodes using a generic list data structure to store the children. Inductive LTree : Set := Node…
Gilles 'SO- stop being evil'
  • 44,159
  • 8
  • 120
  • 184
20
votes
3 answers

Why are loops faster than recursion?

In practice I understand that any recursion can be written as a loop (and vice versa(?)) and if we measure with actual computers we find that loops are faster than recursion for the same problem. But is there any theory what makes this difference or…
Niklas Rosencrantz
  • 1,016
  • 1
  • 9
  • 22
18
votes
3 answers

Does the Y combinator contradict the Curry-Howard correspondence?

The Y combinator has the type $(a \rightarrow a) \rightarrow a$. By the Curry-Howard Correspondence, because the type $(a \rightarrow a) \rightarrow a$ is inhabited, it must correspond to a true theorem. However $a \rightarrow a$ is always true, so…
18
votes
3 answers

Can a tree be traversed without recursion, stack, or queue, and just a handful of pointers?

Half a decade ago I was sitting in a data structures class where the professor offered extra credit if anyone could traverse a tree without using recursion, a stack, queue, etc. (or any other similar data structures) and just a few pointers. I came…
16
votes
2 answers

What property of cons allows elimination of tail recursion modulo cons?

I'm familiar with the idea of basic tail recursion elimination, where functions that return the direct result of a call to themselves can be rewritten as iterative loops. foo(...): # ... return foo(...) I also understand that, as a special…
Maxpm
  • 263
  • 1
  • 7
15
votes
6 answers

Examples of sophisticated recursive algorithms

I was explaining the famous deterministic linear-time selection algorithm (median of medians algorithm) to a friend. The recursion in this algorithm (while being very simple) is quite sophisticated. There are two recursive calls, each with different…
elektronaj
  • 151
  • 1
  • 4
15
votes
1 answer

Is this a generic way to convert any recursive procedure to tail-recursion?

It seems that I've found a generic way to convert any recursive procedure to tail-recursion: Define a helper sub-procedure with an extra "result" parameter. Apply what would be applied to the procedure's return value to that parameter. Call this…
nalzok
  • 1,111
  • 11
  • 21
14
votes
2 answers

Will this program terminate for every Integer?

In a Part Test for GATE Preparation there was a question : f(n): if n is even: f(n) = n/2 else f(n) = f(f(n-1)) I answered "It will terminate for all integers", because even for some negative integers, it will terminate as Stack Overflow…
Prakhar Londhe
  • 251
  • 2
  • 7
13
votes
4 answers

Why is tail recursion better than regular recursion?

There is the axiom you should always prefer tail-recursion over regular recursion whenever possible. (I'm not considering tabulation as an alternative in this question). I understand the idea by why is that the case? Is it only because of the…
AmandaSai98b
  • 251
  • 2
  • 5
13
votes
4 answers

Complexity of recursive Fibonacci algorithm

Using the following recursive Fibonacci algorithm: def fib(n): if n==0: return 0 elif n==1 return 1 return (fib(n-1)+fib(n-2)) If I input the number 5 to find fib(5), I know this will output 5 but how do I examine the…
joker
  • 469
  • 1
  • 5
  • 15
12
votes
2 answers

How to derive dependently typed eliminators?

In dependently-typed programming, there are two main ways of decomposing data and performing recursion: Dependent pattern matching: function definitions are given as multiple clauses. Unification ensures that all omitted cases are impossible, and…
1
2 3
40 41