Questions tagged [tail-call-optimization]

A tail-call optimization is when a function returns directly the result of a called function, to avoid allocating a new stack frame. It is especially useful in recursive functions.

A tail-call optimization is when a function returns directly the result of a called function, to avoid allocating a new stack frame. It is especially useful in recursive functions.

This Stack Overflow question gives more insight: What Is Tail Call Optimization?

172 questions
22
votes
4 answers

Why is Clojure much faster than Scala on a recursive add function?

A friend gave me this code snippet in Clojure (defn sum [coll acc] (if (empty? coll) acc (recur (rest coll) (+ (first coll) acc)))) (time (sum (range 1 9999999) 0)) and asked me how does it fare against a similar Scala implementation. The Scala…
21
votes
3 answers

What are some good ways of implementing tail call elimination?

I've written a small Scheme interpreter in an unholy mix of C/C++, but I have yet to implement proper tail calls. I am aware of the classic Cheney on the MTA algorithm, but are there other nice ways of implementing this? I know I could put the…
csl
  • 10,937
  • 5
  • 57
  • 89
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
8 answers

Differences between JVM implementations

Where do JVM Implementations differ (except licensing)? Does every JVM implement Type Erasure for the Generic handling? Where are the differences between: JRockit IBM JVM SUN JVM Open JDK Blackdown Kaffe ..... Deals one of them with…
Martin K.
  • 4,669
  • 7
  • 35
  • 49
19
votes
1 answer

Will a properly implemented recursive lazy iterator function never stack overflow?

tl;dr; In C#, do you have guarantees that a lazy iterator function that calls nothing but itself and does have a valid recursion exit condition will not cause a stack overflow? Detailed question: I know that as a rule you don't get guarantees of…
Jimmy Hoffa
  • 5,909
  • 30
  • 53
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
17
votes
1 answer

Can/does the (forward) pipe operator prevent tail call optimization?

For a parameter optimization problem at work I wrote a genetic algorithm to find some good settings because a brute-force solution is unfeasible. Unfortunately, when I return in the morning, most of the time I'm presented with a…
primfaktor
  • 2,831
  • 25
  • 34
17
votes
1 answer

Tail Call Elimination in Clojure?

Can somebody rewrite this (plt) Scheme code into Clojure? (define (f n) (printf "(f ~a)~n" n) (g n)) (define (g n) (printf "(g ~a)~n" n) (h n)) (define (h n) (printf "(h ~a)~n" n) (f (+ n 1))) In such a way as to not collapse…
tkf
  • 997
  • 1
  • 7
  • 16
16
votes
2 answers

How do I know if a function is tail recursive in F#

I wrote the follwing function: let str2lst str = let rec f s acc = match s with | "" -> acc | _ -> f (s.Substring 1) (s.[0]::acc) f str [] How can I know if the F# compiler turned it into a loop? Is there a way to…
Dave Berk
  • 1,591
  • 2
  • 15
  • 17
16
votes
2 answers

Does Java 8 have tail call optimization?

I tried digging on the web to get my question answered. I found some documents related to Project DaVinci. This is tagged to the JSR 292 which is related to including closures in the JVM. Did this project get realized and is it a part of Java 8?
Abhijeet Kushe
  • 2,477
  • 3
  • 26
  • 39
14
votes
2 answers

Does MATLAB perform tail call optimization?

I've recently learned Haskell, and am trying to carry the pure functional style over to my other code when possible. An important aspect of this is treating all variables as immutable, i.e. constants. In order to do so, many computations that would…
14
votes
1 answer

tail call optimization in lua

Lua claims that it implement tail call properly thus no stack needs to be maintained for each call thus allow infinite recursion, I tried to write a sum function, one is not tail call, and one is tail call: non tailcall version function sum(n) …
Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72
13
votes
2 answers

Why does tail call optimization need an op code?

So I've read many times before that technically .NET does support tail call optimization (TCO) because it has the opcode for it, and just C# doesn't generate it. I'm not exactly sure why TCO needs an opcode or what it would do. As far as I know, the…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
12
votes
1 answer

Why is tail call optimization not occurring here?

We are using recursion to find factors and are receiving a StackOverflow exception. We've read that the C# compiler on x64 computers performs tail call optimizations: JIT definitely does tailcals when running optimized code and not…
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
12
votes
1 answer

What is the purpose of the extra ldnull and tail. in F# implementation vs C#?

The following C# function: T ResultOfFunc(Func f) { return f(); } compiles unsurprisingly to this: IL_0000: ldarg.1 IL_0001: callvirt 05 00 00 0A IL_0006: ret But the equivalent F# function: let resultOfFunc func =…
Asik
  • 21,506
  • 6
  • 72
  • 131
1
2
3
11 12