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
11
votes
2 answers

Tail optimization guarantee - loop encoding in Haskell

So the short version of my question is, how are we supposed to encode loops in Haskell, in general? There is no tail optimization guarantee in Haskell, bang patterns aren't even a part of the standard (right?), and fold/unfold paradigm is not…
Will Ness
  • 70,110
  • 9
  • 98
  • 181
11
votes
2 answers

F# tail call optimization with 2 recursive calls?

As I was writing this function I knew that I wouldn't get tail call optimization. I still haven't come up with a good way of handling this and was hoping someone else might offer suggestions. I've got a tree: type Heap<'a> = | E | T of int * 'a *…
11
votes
7 answers

Is it possible to force tail call optimization on GCC/Clang?

I'm trying to write a program in functional style with C as much as possible. I know fine compilers like GCC/Clang do tail call optimization silently, but it's not guaranteed. Is there any option to force tail call optimization on the compilers? (Of…
eonil
  • 83,476
  • 81
  • 317
  • 516
10
votes
4 answers

Is it possible to use continuations to make foldRight tail recursive?

The following blog article shows how in F# foldBack can be made tail recursive using continuation passing style. In Scala this would mean that: def foldBack[T,U](l: List[T], acc: U)(f: (T, U) => U): U = { l match { case x :: xs => f(x,…
huynhjl
  • 41,520
  • 14
  • 105
  • 158
10
votes
2 answers

FoldRight over Infinite Structures in Scala using Trampolines

Let's start with a straightforward definition of foldRight: def foldRight[T, U](base: U)(f: (T, => U) => U)(as: Seq[T]): U = { as match { case Nil => base case head +: next => f(head, foldRight(base)(f)(next)) } } One of the advantages…
Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
10
votes
5 answers

vs2010 c++ tail call optimization

Consider the following code: int fac_aux( int x, int res ) { if( x == 1 ) return res; else return fac_aux( x - 1, res * x ); } int fac( int x ) { return fac_aux( x, 1 ); } int main() { int x = fac( 50 ); std::cout << x; …
10
votes
2 answers

Is the lack of tail call optimization an obstacle when using the Eventually workflow?

I'm using a modified version of the Eventually workflow from the F# spec for my development on Xbox. The .net framework on the Xbox does not support tail calls, it seems. Because of this, I have to disable tail call optimization when…
Joh
  • 2,380
  • 20
  • 31
10
votes
2 answers

Doing tail recursion in C++

My function can be written much more simply if I do a tail call recursion (as opposed to a for (;;)...break loop). Yet I'm afraid I'll have performance problems if the compiler fails to optimize it, especially because it will be compiled by the end…
SRobertJames
  • 8,210
  • 14
  • 60
  • 107
10
votes
3 answers

F# has tail call elimination?

In this talk, in the first 8 minutes, Runar explains that Scala has problems with tail call elimination, this makes me wonder whether F# has similar problems ? If not, why not ?
jhegedus
  • 20,244
  • 16
  • 99
  • 167
9
votes
2 answers

Why is this F# sequence function not tail recursive?

Disclosure: this came up in FsCheck, an F# random testing framework I maintain. I have a solution, but I do not like it. Moreover, I do not understand the problem - it was merely circumvented. A fairly standard implementation of (monadic, if we're…
Kurt Schelfthout
  • 8,880
  • 1
  • 30
  • 48
9
votes
5 answers

tail-recursive function appending element to list

i've seen several examples of implementing append an element to a list, but all are not using tail recursion. how to implement such a function in a functional style? (define (append-list lst elem) expr)
8
votes
3 answers

Optimizations by compiler in a recursive program

I got motivated from tail call optimization question What Is Tail Call Optimization? So, I decided to see how can I do it in plain C. So, I wrote the 2 factorial programs, 1st where tail call optimization can be applied. I call this fact function…
8
votes
1 answer

What is the current state of tail-call-optimization for F# on Mono (2.11)?

What is the current state of Tail Call Optimization (TCO) implementation on Mono (2.11) ? Read somewhere that all the codebase would need to be modified to use a callee-pops-arguments convention. What is the status of this change ? Is the ARM/Linux…
8
votes
1 answer

About JSLint, its dislike of for loops, and tail call optimization

I noticed that the new version of JSLint doesn't like some forms of for loops. I found that to be strange, and started digging for some explanation. Under JsLint's help page, you can find this: The most important new feature of ES6 is proper tail…
user4698813
8
votes
2 answers

Why does the F# compiler not create a tail call for this function?

I'm having trouble with the fixed point combinator in F#: let rec fix f a = f (fix f) a fix (fun body num -> if num = 1000000 then System.Console.WriteLine "Done!" else body (num + 1) ) 0 (This code is just to demonstrate the…
1 2
3
11 12