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
0
votes
1 answer

How can I tell if my function has been tail call optimized?

I'm reading section 2.8 (Tail Recursion) in On Lisp. It has an example of a tail recursive function: (defun our-length-tr (lst) "tail recursive version with accumulator" (labels ((rec (lst acc) (if (null lst) acc …
Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
0
votes
1 answer

Is continuation style implementation of factorial tail call optimized (TCO)?

Here are two factorial implementation from this site: Tail Call Optimized (TCO): function fact(n) { return tail_fact(n,1) ; } function tail_fact(n,a) { if (n == 0) return a ; else return tail_fact(n-1,n*a) ; } And the one rewritten…
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
0
votes
1 answer

Haskell: TCO and Lazy evaluation

I'm trying to understand why the first main does not terminate when c is not valid, while the second terminates. From the description here main is just an unevaluated thunk, and executing it is just building up the data structure. I'm trying to…
user3169543
  • 1,499
  • 1
  • 10
  • 16
0
votes
1 answer

How are proper tail calls enabled in ES5/strict mode?

Today, I was reading the harmony:proper_tail_calls proposal and I noticed that in the references there was a link which read, “Brendan discovers that ES5/strict enables TCO.” What does it mean that ES5/strict “enables” TCO? At first I thought that…
0
votes
1 answer

F# tail call broken by lack of parentheses

While testing F# tail calls with F# team blog article I found that almost the same code has the same result but different IL although only parentheses in the code differ. Next code is optimized by compiler and I see br.s IL_0000 in the end of IL and…
0
votes
2 answers

How to achieve tail call optimization while traversing tree-like structure using continuation-passing style

I try to implement tail call optimization to traverse tree-line structure using continuation-passing style in scala. Unfortunately my previous experience with fsharp does not help much. I have recursive call w/o tail optimization: def…
Akim
  • 8,469
  • 2
  • 31
  • 49
-2
votes
1 answer

Why is this tail-call optimized method not recognized as such by the Scala compiler?

This simple regex implementation (scastie here) does not compile, where I expected it to. The error is at line 14, where an intermediate recursive call is interpreted as to break the @tailrec requirement. While this intermediate recursive call is…
1 2 3
11
12