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

Clojure: Avoiding stack overflow in Sieve of Erathosthene?

Here's my implementation of Sieve of Erathosthene in Clojure (based on SICP lesson on streams): (defn nats-from [n] (iterate inc n)) (defn divide? [p q] (zero? (rem q p))) (defn sieve [stream] (lazy-seq (cons (first stream) …
nixx
  • 121
  • 2
12
votes
3 answers

Prolog Accumulators. Are they really a "different" concept?

I am learning Prolog under my Artificial Intelligence Lab, from the source Learn Prolog Now!. In the 5th Chapter we come to learn about Accumulators. And as an example, these two code snippets are given. To Find the Length of a List without…
tmj
  • 1,750
  • 2
  • 19
  • 34
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
4 answers

F# tail recursion and why not write a while loop?

I'm learning F# (new to functional programming in general though used functional aspects of C# for years but let's face it, that's pretty different) and one of the things that I've read is that the F# compiler identifies tail recursion and compiles…
hackerhasid
  • 11,699
  • 10
  • 42
  • 60
11
votes
5 answers

Use of recursion in Scala when run in the JVM

From searching elsewhere on this site and the web, tail call optimization is not supported by the JVM. Does that therefore mean that tail recursive Scala code such as the following, which may run on very large input lists, should not be written if…
gw111zz
  • 274
  • 2
  • 10
11
votes
8 answers

Problem with Tail Recursion in g++

I'm was messing around with tail-recursive functions in C++, and I've run into a bit of a snag with the g++ compiler. The following code results in a stack overflow when numbers[] is over a couple hundred integers in size. Examining the assembly…
Swiss
  • 5,556
  • 1
  • 28
  • 42
11
votes
1 answer

Tail recursive fold on a binary tree in Scala

I am trying to find a tail recursive fold function for a binary tree. Given the following definitions: // From the book "Functional Programming in Scala", page 45 sealed trait Tree[+A] case class Leaf[A](value: A) extends Tree[A] case class…
Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
11
votes
5 answers

Recursion overhead -- how serious is it?

Possible Duplicate: Is recursion ever faster than looping? I was first trained to program seriously in C, about 15 years ago. My employer wanted highly optimized code for computationally difficult tasks. I remember being advised more than once…
11
votes
3 answers

Verify that an OCaml function is tail-recursive

How can I tell if OCaml recognizes a particular function as tail-recursive? In particular, I want to find out if the OCaml compiler recognizes Short-circuited operators and tail recursion Thanks to Jeffrey's answer below, I tried this with the…
dspyz
  • 5,280
  • 2
  • 25
  • 63
11
votes
4 answers

Avoiding code duplication in F#

I have two snippets of code that tries to convert a float list to a Vector3 or Vector2 list. The idea is to take 2/3 elements at a time from the list and combine them as a vector. The end result is a sequence of vectors. let rec vec3Seq…
Johan
  • 660
  • 1
  • 6
  • 13
11
votes
3 answers

Haskell: tail recursion version of depth of binary tree

First of all I have two different implementation that I believe are correct, and have profiled them and thinking they are about of the same performance: depth::Tree a -> Int depth Empty = 0 depth (Branch b l r) = 1 + max (depth l) (depth…
Bob Fang
  • 6,963
  • 10
  • 39
  • 72
11
votes
3 answers

tail recursive vs iterative algorithms

I couldn't find articles describing why tail recursive functions should be preferred over iterative algorithms. I'm not asking why tail recursive is better than simple recursive which I think is clearly explained everywhere. So why sum(n) = { …
raisercostin
  • 8,777
  • 5
  • 67
  • 76
10
votes
2 answers

How to make tree mapping tail-recursive?

Suppose I have a tree data structure like this: trait Node { val name: String } case class BranchNode(name: String, children: List[Node]) extends Node case class LeafNode(name: String) extends Node Suppose also I've got a function to map over…
Michael
  • 41,026
  • 70
  • 193
  • 341
10
votes
1 answer

Is there a way to assert that a function is recognized as tail-recursive by the compiler?

Let's say I've written a function in Haskell and I want to assert that it is tail-recursive and it will be optimized by the compiler. Is there a way to do it? I know there's a way to do it in Scala with @tailrec annotation. Example: import…
Mysquff
  • 293
  • 4
  • 11
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