Questions tagged [tail-recursion]

Tail recursion is a recursive strategy in which a function does some amount of work, then invokes itself. The "tail" refers to the fact that the recursion is at the very end of the function. Many -- especially functional -- programming language compilers can turn these types of calls into iteration, meaning tail recursion in supported languages can be used without fear of a stack overflow, regardless of the number of calls.

Tail recursion is a recursive strategy in which a function does some amount of work, then invokes itself. The "tail" refers to the fact that the recursion is at the very end of the function. Many (especially functional language) compilers can turn these types of calls into iterative calls, meaning tail recursion can be used without fear of a stack overflow, regardless of the number of calls.

1348 questions
21
votes
6 answers

Convert normal recursion to tail recursion

I was wondering if there is some general method to convert a "normal" recursion with foo(...) + foo(...) as the last call to a tail-recursion. For example (scala): def pascal(c: Int, r: Int): Int = { if (c == 0 || c == r) 1 else pascal(c - 1, r -…
DennisVDB
  • 1,347
  • 1
  • 14
  • 30
21
votes
7 answers

Can all recursive functions be re-written as tail-recursions?

Possible Duplicate: Are there problems that cannot be written using tail recursion? From my understanding, tail recursion is an optimization you can use when a recursive call does not need information from the recursive calls that it will spam.…
aerain
  • 1,149
  • 3
  • 12
  • 17
20
votes
4 answers

Explain to me what the big deal with tail-call optimization is and why Python needs it

Apparently, there's been a big brouhaha over whether or not Python needs tail-call optimization (TCO). This came to a head when someone shipped Guido a copy of SICP, because he didn't "get it." I'm in the same boat as Guido. I understand the…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
20
votes
3 answers

Why won't Scala optimize tail call with try/catch?

In a recent StackOverflow answer, I gave the following recursive code: def retry[T](n: Int)(fn: => T): T = { try { fn } catch { case e if n > 1 => retry(n - 1)(fn) } } If I add the @tailrec annotation, I get: Could not optimize…
leedm777
  • 23,444
  • 10
  • 58
  • 87
20
votes
2 answers

Why doesn't Haskell need Trampolining?

As Scala developer learning the IO Monad, and therefore technicalities of Trampolining in general that are necessary for recursion where tail call optimization is not possible, I wonder how Haskell seems to natively avoid it. I get that Haskell is a…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
20
votes
2 answers

How to use TailCalls?

If I understand correctly, scala.util.control.TailCalls can be used to avoid stack overflows for non-tail-recursive functions by using a trampoline. The example given in the API is straightforward: import scala.util.control.TailCalls._ def…
Landei
  • 54,104
  • 13
  • 100
  • 195
20
votes
6 answers

What is the advantage of using tail recursion here?

I have been reading articles describing how space complexity of quicksort can be reduced by using the tail recursive version but I am not able to understand how this is so. Following are the two versions : QUICKSORT(A, p, r) q = PARTITION(A,…
vjain27
  • 3,514
  • 9
  • 41
  • 60
20
votes
8 answers

How to think in recursive way?

In order to understand the advanced algorithm concepts like greedy methods and dynamic programming, one first need to be well versed in recursion. I am relatively new to recursion. Whenever a question is asked, the first things that pops up in mind…
Rahul Kurup
  • 693
  • 3
  • 9
  • 22
19
votes
6 answers

Why does F# impose a low limit on stack size?

I would like to know if there is a fundamental reason for limiting the depth of recursion in F# to 10000 or so, and ideally how to avoid that limit. I think it is perfectly reasonable to write code that uses O(n) stack space, and would be grateful…
user1002059
  • 1,525
  • 1
  • 9
  • 19
19
votes
11 answers

Generating Fibonacci series in F#

I'm just starting to learn F# using VS2010 and below is my first attempt at generating the Fibonacci series. What I'm trying to do is to build a list of all numbers less than 400. let fabList = let l = [1;2;] let mutable a = 1 let…
photo_tom
  • 7,292
  • 14
  • 68
  • 116
19
votes
8 answers

Efficient recursion in functional programming vs. inefficient recursion in different paradigms

As far as I know recursion is very elegant but unefficient in OOP and procedural programming (see the wonderful "High Order perl", Mark Jason Dominus). I had some informations that in functional programming recursion is fast - keeping its elegance…
Daniel
  • 1,357
  • 2
  • 19
  • 39
19
votes
6 answers

Quicksort and tail recursive optimization

In Introduction to Algorithms p169 it talks about using tail recursion for Quicksort. The original Quicksort algorithm earlier in the chapter is (in pseudo-code) Quicksort(A, p, r) { if (p < r) { q: <- Partition(A, p, r) Quicksort(A, p, q) …
happygilmore
  • 3,008
  • 4
  • 23
  • 37
19
votes
3 answers

Can a backtracking tail recursive algorithm be converted to iteration?

Let's take the Knight Tour problem. Can that be converted to iteration? What is confunsing me is the backtracking part. How do I backtrack in a loop? Do I have to necessarily use a stack data-structure to implement backtracking when I go from…
chrisapotek
  • 6,007
  • 14
  • 51
  • 85
18
votes
1 answer

How to write tail-recursive functions when working inside monads

In general I have problems figuring out how to write tailrecursive functions when working 'inside' monads. Here is a quick example: This is from a small example application that I am writing to better understand FP in Scala. First of all the user is…
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50
18
votes
1 answer

Does Java support tail recursion?

Possible Duplicate: Why does the JVM still not support tail-call optimization? I see so many different answers online, so I thought I'd ask the experts.
Donald T
  • 10,234
  • 17
  • 63
  • 91