Questions tagged [tail-recursion]

Tail recursion is a recursive strategy in which a function does some amount of work, then invokes itself. The "tail" refers to the fact that the recursion is at the very end of the function. Many -- especially functional -- programming language compilers can turn these types of calls into iteration, meaning tail recursion in supported languages can be used without fear of a stack overflow, regardless of the number of calls.

Tail recursion is a recursive strategy in which a function does some amount of work, then invokes itself. The "tail" refers to the fact that the recursion is at the very end of the function. Many (especially functional language) compilers can turn these types of calls into iterative calls, meaning tail recursion can be used without fear of a stack overflow, regardless of the number of calls.

1348 questions
8
votes
1 answer

Is GHC able to tail-call optimize IO actions?

Will GHC perform tail-call optimization on the following function by default? The only weird thing about it is that it is recursively defining an IO action, but I don't see why this couldn't be TCO'd. import Control.Concurrent.MVar consume :: MVar…
Geoff
  • 873
  • 1
  • 7
  • 14
8
votes
1 answer

Always avoid recursive methods in Java?

I recall that one should always avoid using recursive method calls in Java. I thought the reasons are, that the overhead procuded by saving the invoked methods on the heap is not worth the reduced lines of code in the implementation. However, I was…
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
8
votes
4 answers

Unwinding frame but do not return in C

My programming language compiles to C, I want to implement tail recursion optimization. The question here is how to pass control to another function without "returning" from the current function. It is quite easy if the control is passed to the same…
exebook
  • 32,014
  • 33
  • 141
  • 226
8
votes
2 answers

Is this implementation tail-recursive

I read in an algorithmic book that the Ackermann function cannot be made tail-recursive (what they say is "it can't be transformed into an iteration"). I'm pretty perplex about this, so I tried and come up with this: let Ackb m n = let rec rAck…
Clément
  • 12,299
  • 15
  • 75
  • 115
8
votes
2 answers

does Babel with the `es2016` preset implement tail call optimization?

I used the following example to test tail call recursion with Babel and the es2016 preset: 'use strict'; try { function r(n) { if (n%5000===0) console.log(`reached a depth of ${n}`); r(n+1); } r(0); } catch…
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
8
votes
1 answer

Is this code tail-recursive?

I am trying to write a solution for AdventCode day 6 in Prolog. (http://adventofcode.com/day/6) Previously I wrote a solution that dynamically created and replaced predicates, to keep track of the lights. Unsurprisingly, it's rather slow, so I…
8
votes
2 answers

How to create a recursive data structure value in (functional) F#?

How can a value of type: type Tree = | Node of int * Tree list have a value that references itself generated in a functional way? The resulting value should be equal to x in the following Python code, for a suitable definition of Tree: x =…
Muhammad Alkarouri
  • 23,884
  • 19
  • 66
  • 101
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…
8
votes
1 answer

Understanding F# tail-recursive

Recently, I'm learning F#. I try to solve problem in different ways. Like this: (* [0;1;2;3;4;5;6;7;8] -> [(0,1,2);(3,4,5);(6,7,8)] *) //head-recursive let rec toTriplet_v1 list= match list with | a::b::c::t -> (a,b,c)::(toTriplet_v1 t) …
kev
  • 155,172
  • 47
  • 273
  • 272
8
votes
2 answers

Tail-recursive merge sort in OCaml

I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up with the following code: let tailrec_merge_sort l = let split l = let rec _split source left right = match source with | [] -> (left,…
Clément
  • 12,299
  • 15
  • 75
  • 115
8
votes
2 answers

Do ML family compilers do any sophisticated optimization for tail calls?

I (believe) the following function definition is tail-recursive: fun is_sorted [] = true | is_sorted [x] = true | is_sorted (x::(y::xs)) = if x > y then false else is_sorted (y::xs) Trivially, it's equivalent to the following…
Zach Halle
  • 321
  • 1
  • 9
8
votes
2 answers

Do I need to use @tailrec in Scala?

In the following example of a Scala function: @tailrec def someFunction( ... ): Unit = { Is the @tailrec annotation doing anything useful or is it just nice to know that this is a tail recursion?
JamboShrimp
  • 83
  • 1
  • 4
8
votes
3 answers

Can this be tail call optimized? If so, what's the special reason for it not happen?

I've checked assembly output at many optimization levels with both gcc 4.8.1 and clang 3.4.190255, no tail call optimization for this kind of code. Any special reason why collatz_aux doesn't get a tail call optimization? #include #include…
oblitum
  • 11,380
  • 6
  • 54
  • 120
8
votes
2 answers

Does the CLR .tail instruction disable preemptive GC?

I'm attempting to debug a production issue with a windows service that has a tendency to fall over rapidly once a number of concurrent connections are active. Through the magic of a core dump and DebugDiag I was able to discover that there was a…
ckramer
  • 9,419
  • 1
  • 24
  • 38
8
votes
1 answer

Tailcalls in Mono

I have some code in F# that works fine under .net but overflows the stack under Mono. A related issue is that it seems to do so long before the stack space supposedly available to it runs out (it is started with System.Threading.Thread (ts,…
Joe Huha
  • 548
  • 3
  • 16