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

Erlang: stackoverflow with recursive function that is not tail call optimized?

Is it possible to get a stackoverflow with a function that is not tail call optimized in Erlang? For example, suppose I have a function like this sum_list([],Acc) -> Acc; sum_list([Head|Tail],Acc) -> Head + sum_list(Tail, Acc). It would seem…
Stratus3D
  • 4,648
  • 4
  • 35
  • 67
7
votes
2 answers

Compiling Tail-Call Optimization In Mutual Recursion Across C and Haskell

I'm experimenting with the foreign-function interface in Haskell. I wanted to implement a simple test to see if I could do mutual recursion. So, I created the following Haskell code: module MutualRecursion where import Data.Int foreign import ccall…
7
votes
1 answer

Pass-by-reference hinders gcc from tail call elimination

See BlendingTable::create and BlendingTable::print. Both have the same form of tail recursion, but while create will be optimized as a loop, print will not and cause a stack overflow. Go down to see a fix, which I got from a hint from one of the gcc…
user3810155
7
votes
1 answer

What's some simple F# code that generates the .tail IL instruction?

I'd like to see the .tail IL instruction, but the simple recursive functions using tail calls that I've been writing are apparently optimized into loops. I'm actually guessing on this, as I'm not entirely sure what a loop looks like in Reflector. …
kld2010
  • 73
  • 2
7
votes
1 answer

Rebol Tail Call Optimization

I come from a functional programming background and think first about recursive solutions to problems rather than iterative ones. I'm starting to work with Rebol a bit (specifically R3) and have written a solution to the primefactor kata using a…
Jamie
  • 150
  • 6
7
votes
2 answers

Why does this code prevent gcc & llvm from tail-call optimization?

I have tried the following code on gcc 4.4.5 on Linux and gcc-llvm on Mac OSX(Xcode 4.2.1) and this. The below are the source and the generated disassembly of the relevant functions. (Added: compiled with gcc -O2 main.c) #include…
user1937358
  • 239
  • 1
  • 2
  • 8
7
votes
2 answers

Is my rewritten foldl function optimised?

I just started Haskell 2 days ago so I'm not yet sure about how to optimise my code. As an exercise, I have rewritten foldl and foldr ( I will give foldl here but foldr is the same, replacing last with head and init with tail). The code is: module…
GabrielG
  • 189
  • 1
  • 9
6
votes
1 answer

Why is the tail call optimization not used in this Haskell program?

The following program blows the stack: __find_first_occurrence :: (Eq b) => b -> [b] -> Int -> Int __find_first_occurrence e [] i = -1 __find_first_occurrence e (x:xs) i | e == x = i | otherwise = __find_first_occurrence e xs (i +…
quant_dev
  • 6,181
  • 1
  • 34
  • 57
6
votes
2 answers

Tail recursion with Groovy

I coded 3 factorial algorithms: I expect to fail by stack overflow. No problem. I try a tail recursive call, and convert the previous algorithm from recursive to iterative. It doesn't work, but I don't understand why. I use trampoline() method and…
6
votes
1 answer

Why does gcc perform tail call optimization for one version but not for the other?

Experimenting with tail call optimization (tco), I stumbled upon the following curious example: unsigned long long int fac1(unsigned long long int n){ if (n==0) return 1; return n*fac1(n-1); } actually, I was impressed, that gcc was able to…
ead
  • 32,758
  • 6
  • 90
  • 153
6
votes
1 answer

Elixir tail-call recursive function

I have this function that finds the even numbers in a list and returns a new list with only those numbers: def even([]), do: [] def even([head | tail]) when rem(head, 2) == 0 do [head | even(tail)] end def even([_head| tail]) do …
JAbrams
  • 325
  • 4
  • 7
  • 18
6
votes
2 answers

What is the difference between loop/recur and recur by itself?

I'm having trouble finding the answer to this in the Clojure docs. I'm new to Clojure and it seems as though you can use recur in two different ways and essentially get the same result. Example 1: (defn my-function [num] (if (> num 10) num …
rescuecreative
  • 3,607
  • 3
  • 18
  • 28
6
votes
1 answer

Why do no javascript engines support tail call optimization?

I recently learned about tail call optimization in Haskell. I've learned from the following posts that this is not a feature of javascript: Tail Recursion optimization for JavaScript? Are any Javascript engines tail call optimized? Is there…
6
votes
2 answers

Is this a tail call? (Javascript)

Supose you have a recursive function like: Blah.prototype.add = function(n) { this.total += n; this.children.forEach(function(child) { child.add(n); }); }; Is the child.add() a tail call? If not can it be written so it is?
pixelmike
  • 1,973
  • 1
  • 19
  • 31
6
votes
2 answers

How do I detect functions that I can apply tail call optimisation to?

I'm trying to write a Scheme interpreter, but finding TCO difficult to implement. I'm not sure what properties a function must have in order for TCO to kick in. 1) A function with a self-recursive call at the end of the definition: (define (sum-list…
Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192