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
76
votes
3 answers

Why does a recursive call cause StackOverflow at different stack depths?

I was trying to figure out hands-on how tail calls are handled by the C# compiler. (Answer: They're not. But the 64bit JIT(s) WILL do TCE (tail call elimination). Restrictions apply.) So I wrote a small test using a recursive call that prints how…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
69
votes
10 answers

How do I check if gcc is performing tail-recursion optimization?

How do I tell if gcc (more specifically, g++) is optimizing tail recursion in a particular function? (Because it's come up a few times: I don't want to test if gcc can optimize tail recursion in general. I want to know if it optimizes my tail…
A. Rex
  • 31,633
  • 21
  • 89
  • 96
68
votes
6 answers

Tail recursion in C++

Can someone show me a simple tail-recursive function in C++? Why is tail recursion better, if it even is? What other kinds of recursion are there besides tail recursion?
neuromancer
  • 53,769
  • 78
  • 166
  • 223
67
votes
2 answers

F# vs OCaml: Stack overflow

I recently found a presentation about F# for Python programmers, and after watching it, I decided to implement a solution to the "ant puzzle" on my own. There is an ant that can walk around on a planar grid. The ant can move one space at a time…
ttsiodras
  • 10,602
  • 6
  • 55
  • 71
66
votes
4 answers

Does Scala support tail recursion optimization?

Does Scala support tail recursion optimization?
Roman Kagan
  • 10,440
  • 26
  • 86
  • 126
54
votes
2 answers

When is tail recursion guaranteed in Rust?

C language In the C programming language, it's easy to have tail recursion: int foo(...) { return foo(...); } Just return as is the return value of the recursive call. It is especially important when this recursion may repeat a thousand or even…
uben
  • 1,221
  • 2
  • 11
  • 20
54
votes
5 answers

Are there problems that cannot be written using tail recursion?

Tail recursion is an important performance optimisation stragegy in functional languages because it allows recursive calls to consume constant stack (rather than O(n)). Are there any problems that simply cannot be written in a tail-recursive style,…
ctford
  • 7,189
  • 4
  • 34
  • 51
51
votes
4 answers

Tail Call Optimization in Go

Does the Go programming language, as of now, optimize tail calls? If not, does it at least optimize tail-recursive calls of a function to itself?
dxuhuang
  • 990
  • 1
  • 9
  • 18
48
votes
2 answers

Isn't that code in tail recursive style?

I'm kinda new to Scala trying it out while reading Beggining Scala by David Pollack. He defines a simple recursive function that loads all strings from the file: def allStrings(expr: => String): List[String] = expr match { case null => Nil …
Grozz
  • 8,317
  • 4
  • 38
  • 53
48
votes
6 answers

How does Haskell tail recursion work?

I wrote this snippet of code and I assume len is tail-recursive, but a stack overflow still occurs. What is wrong? myLength :: [a] -> Integer myLength xs = len xs 0 where len [] l = l len (x:xs) l = len xs (l+1) main = print $…
Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73
46
votes
5 answers

Why won't the Scala compiler apply tail call optimization unless a method is final?

Why won't the Scala compiler apply tail call optimization unless a method is final? For example, this: class C { @tailrec def fact(n: Int, result: Int): Int = if(n == 0) result else fact(n - 1, n *…
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
43
votes
2 answers

Tail Call Optimisation in Java

As of Java 8, Java does not provide Tail-Call Optimization (TCO). On researching about it, I came to know the reason which is: In JDK classes [...] there are a number of security sensitive methods that rely on counting stack frames between JDK…
Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
43
votes
5 answers

F# Tail Recursive Function Example

I am new to F# and was reading about tail recursive functions and was hoping someone could give me two different implementations of a function foo - one that is tail recursive and one that isn't so that I can better understand the principle.
Mark Pearl
  • 7,573
  • 10
  • 47
  • 57
43
votes
3 answers

Generate tail call opcode

Out of curiosity I was trying to generate a tail call opcode using C#. Fibinacci is an easy one, so my c# example looks like this: private static void Main(string[] args) { Console.WriteLine(Fib(int.MaxValue, 0)); } public…
devshorts
  • 8,572
  • 4
  • 50
  • 73
38
votes
2 answers

What is tail-recursion elimination?

Steve Yegge mentioned it in a blog post and I have no idea what it means, could someone fill me in? Is it the same thing as tail call optimization?
1
2
3
89 90