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
1
vote
0 answers

Ensuring tail-call optimization in C

Possible Duplicate: Which, if any, C++ compilers do tail-recursion optimization? I'm writing a small runtime for a Lisp-like language for educational purposes, but I'd like to make sure the C code is tail-call optimized, since I'm depending on it…
Alexis King
  • 43,109
  • 15
  • 131
  • 205
1
vote
1 answer

Does PL/SQL perform tail call optimization?

I'm fairly new to the language, and I was wondering if tail calls were optimized. In other language I could examinate the machine code or an intermediate representation and figure it for myself but I have no idea about how to do that in…
1
vote
2 answers

Is this F# function tail-recursive where the recursive function is called several times inside the function?

There are a couple of questions about tail-recursive function e.g. this and this but could not find anything similar to the following. My understanding is that a tail-call optimised function should return an accumulated value in its last call…
vis
  • 2,279
  • 1
  • 19
  • 27
1
vote
2 answers

Does Xcode for the iPhone eliminate tail-call recursion?

Does Xcode support tail-call optimization on the iPhone?
Bill
  • 44,502
  • 24
  • 122
  • 213
0
votes
1 answer

How to do tail call optimisation in Scala3?

I am trying to write a program that is 100% iterative, that is, the functions never need to return, because nothing must happen after such a return. In other words, the program is 100% in tail position. Consider the following toy program: def…
0
votes
1 answer

Is tail call (including tail recursion) compiler/implementation dependent?

Searching tail recursion on the internet I stumbled on How does compiler know whether the recursion is a tail recursion or not and how does it optimize tail recursion. If I understand correctly then the reason for tail recursion being faster than…
0
votes
0 answers

Tail call optimization for procs possible?

I see that tail call optimization can be enabled in ruby and that it works with method calls following these instructions, but I need it to work for Procs too for use with blocks/lambdas, since only these can pass closures around as first class…
Darren Smith
  • 207
  • 1
  • 4
0
votes
1 answer

Why does clang have trouble with optimizing tail calls in destructors?

Here is a simplified singly-linked list, where each node owns the next, along with a function for destroying the list: struct Node { Node* next = nullptr; ~Node() { delete next; } }; void Destroy(Node* head) { delete head; } Clang…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
0
votes
3 answers

How can i transform this scala function in order to be optimized

Code to determine the lat element of a list, using pattern matching: @tailrec def last_rec[A](list : List[A]) : A = { list match { case (x :: Nil) => x case (_ :: xs) => last_rec(xs) case Nil => throw new…
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
0
votes
1 answer

Tail call stack

I'm having trouble understanding the Stack manipulation needed in order to implement Tail call in assembly language. When we have a Tail call to function We basically want to override the current Activation Frame with the Activation Frame of the…
0
votes
1 answer

Tail call optimization with eigen

I'm trying to make the compiler tail-call optimize eigen-expressions that are passed recursively. The following will optimize using GCC -O2: inline double recursive(const double &A, const double &b, …
Anon232
  • 3
  • 6
0
votes
1 answer

print a newline using tail call optimization

There's this unanswered question in the Igor Zhirkov's book Low-Level Programming : "Try to rewrite print_newline without calling print_char or copying its code. Hint: read about tail call optimization.". I've spent some times reading about tail…
trogne
  • 3,402
  • 3
  • 33
  • 50
0
votes
2 answers

Can JavaScript function call subexpressions be tail calls?

Consider the following return statement: return f() || g(); The call f() obviously is not a tail call, because the function does not actually return if f() is falsy. What about the g() part though, is that a tail call? Or do I have to rewrite it…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
0
votes
2 answers

Get results for sub problems using tail call recursion in Scala

I am trying to calculate results for each sub problem using @tailrec similar to how normal recursive solutions can produce solutions for each sub problem. Following is the example I worked on. @tailrec def collatz( n: BigInt, acc:…
Hariharan
  • 881
  • 1
  • 13
  • 25
0
votes
1 answer

Telling GCC to tail-call a function

Let's say I've got a C function: unsigned int fact(unsigned int n, unsigned int acc) { if(n > 0) return fact(n - 1, n * acc); return acc; } Is there a way to tell GCC to tail call the recursive function, something like unsigned int…
kerkeslager
  • 1,364
  • 4
  • 17
  • 34
1 2 3
11
12