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
37
votes
2 answers

Does C# do tail recursion?

Possible Duplicate: Why doesn't .net/C# eliminate tail recursion? Does C# do tail recusion? I can't find any documentation telling me if it does or not.
Vilor
  • 371
  • 1
  • 3
  • 3
36
votes
3 answers

Does PHP optimize tail recursion?

I wrote a small piece of code that I believe should have succeeded if tail recursion was optimized, however it blew up the stack. Should I conclude PHP does not optimize tail recursion? function sumrand($n,$sum) { if ($n== 0) { return…
Max
  • 363
  • 3
  • 4
35
votes
2 answers

Why is my Scala tail-recursion faster than the while loop?

Here are two solutions to exercise 4.9 in Cay Horstmann's Scala for the Impatient: "Write a function lteqgt(values: Array[Int], v: Int) that returns a triple containing the counts of values less than v, equal to v, and greater than v." One uses tail…
waifnstray
  • 547
  • 5
  • 6
35
votes
8 answers

C tail call optimization

I often hear people say that C doesn't perform tail call elimination. Even though it's not guaranteed by the standard, isn't it performed in practice by any decent implementation anyhow? Assuming you're only targeting mature, well implemented…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
33
votes
2 answers

What exactly is tail position for recur?

What's the precise definition of "tail position" for recur in Clojure? I would think that it would be the last item in a loop S-expression, but in the example below it seems to me that the S-Expression which starts with (if ...) is in tail position…
tjb
  • 11,480
  • 9
  • 70
  • 91
32
votes
3 answers

Avoiding stack overflow (with F# infinite sequences of sequences)

I have this "learning code" I wrote for the morris seq in f# that suffers from stack overflow that I don't know how to avoid. "morris" returns an infinite sequence of "see and say" sequences (i.e., {{1}, {1,1}, {2,1}, {1,2,1,1}, {1,1,1,2,2,1},…
Tony Lee
  • 5,622
  • 1
  • 28
  • 45
32
votes
5 answers

Combine memoization and tail-recursion

Is it possible to combine memoization and tail-recursion somehow? I'm learning F# at the moment and understand both concepts but can't seem to combine them. Suppose I have the following memoize function (from Real-World Functional Programming): let…
Ronald Wildenberg
  • 31,634
  • 14
  • 90
  • 133
32
votes
3 answers

What does "sibling calls" mean?

On GCC manual, -foptimize-sibling-calls Optimize sibling and tail recursive calls. I know tail recursive calls, for example int sum(int n) { return n == 1 ? 1 : n + sum(n-1); } However, what does sibling calls mean?
buaagg
  • 599
  • 6
  • 11
31
votes
2 answers

Can Tail Call Optimization and RAII Co-Exist?

I can't think of a true RAII language that also has tail call optimization in the specs, but I know many C++ implementations can do it as an implementation-specific optimization. This poses a question for those implementations that do: given that…
29
votes
2 answers

Tail call optimization in Mathematica?

While formulating an answer to another SO question, I came across some strange behaviour regarding tail recursion in Mathematica. The Mathematica documentation hints that tail call optimization might be performed. But my own experiments give…
28
votes
2 answers

Under what circumstances are monadic computations tail-recursive?

In Haskell Wiki's Recursion in a monad there is an example that is claimed to be tail-recursive: f 0 acc = return (reverse acc) f n acc = do v <- getLine f (n-1) (v : acc) While the imperative notation leads us to believe that it is…
Petr
  • 62,528
  • 13
  • 153
  • 317
22
votes
6 answers

Reverse list Scala

Given the following code: import scala.util.Random object Reverser { // Fails for big list def reverseList[A](list : List[A]) : List[A] = { list match { case Nil => list case (x :: xs) => reverseList(xs) ::: List(x) } } …
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
22
votes
2 answers

Tail Recursion in Haskell

I am trying to understand tail-recursion in Haskell. I think I understand what it is and how it works but I'd like to make sure I am not messing things up. Here is the "standard" factorial definition: factorial 1 = 1 factorial k = k * factorial…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
22
votes
4 answers

Why is Clojure much faster than Scala on a recursive add function?

A friend gave me this code snippet in Clojure (defn sum [coll acc] (if (empty? coll) acc (recur (rest coll) (+ (first coll) acc)))) (time (sum (range 1 9999999) 0)) and asked me how does it fare against a similar Scala implementation. The Scala…
22
votes
5 answers

How to do recursion in anonymous fn, without tail recursion

How do I do recursion in an anonymous function, without using tail recursion? For example (from Vanderhart 2010, p 38): (defn power [number exponent] (if (zero? exponent) 1 (* number (power number (- exponent 1))))) Let's say I wanted…
1 2
3
89 90