tail-recursion

What is tail-recursion?

Whilst starting to learn lisp, I've come across the term tail-recursive. What does it mean? ...

Which, if any, C++ compilers do tail-recursion optimization?

It seems to me that it would work perfectly well to do tail-recursion optimization in both C and C++, yet while debugging I never seem to see a frame stack that indicates this optimization. That is kind of good, because the stack tells me how deep the recursion is. However, the optimization would be kind of nice as well. Do any C++ comp...

Does the JVM prevent tail call optimizations?

I saw this on a question: Scala in particular doesn't support tail-call elimination except in self-recursive functions, which limits the kinds of composition you can do (this is a fundamental limitation of the JVM). Is this true? If so, what is it about the JVM that creates this fundamental limitation? ...

Which languages support tail recursion optimization?

which languages support tail recursion optimization? ...

How does Haskell tail recursion work?

I wrote this snippet of code and I assume len is tail-recursive, but a stack overflow still occurs. What is wrong? myLength :: [a] -> Integer myLength xs = len xs 0 where len [] l = l len (x:xs) l = len xs (l+1) main = print $ myLength [1..10000000] ...

What is the best approach for a tail-optimized function for calculating the length of a list?

Here is an example that a forum poster gave, I can't tell if this tail optimized. Also, could someone give a laymans description of how a tail optimized version would trump the normal version. (defun mylength (s) (labels ((mylength-inner (s x) (if (car s) (mylength-inner (cdr s) (+ x 1)) x))) (mylength-inner s ...

How do I check if gcc is performing tail-recursion optimization?

How do I tell if gcc (more specifically, g++) is optimizing tail recursion in a particular function? (Because it's come up a few times: I don't want to test if gcc can optimize tail recursion in general. I want to know if it optimizes my tail recursive function.) If your answer is "look at the generated assembler", I'd like to know ex...

Why doesn't .net/C# eliminate tail recursion?

I found this question about which languages optimize tail recursion. What I want to know is why C# doesn't optimize tail recursion, whenever possible? For a concrete case, why isn't this method optimized into a loop (VS2008 32 bit, if that matters)?: private static void Foo(int i) { if (i == 1000000) return; if (i % 100 == 0) ...

Should I avoid recursion on the iPhone?

Should I avoid recursion with code that runs on the iPhone? Or put another way, does anyone know the max stack size on the iphone? ...

How do I know if a function is tail recursive in F#

I wrote the follwing function: let str2lst str = let rec f s acc = match s with | "" -> acc | _ -> f (s.Substring 1) (s.[0]::acc) f str [] How can I know if the F# compiler turned it into a loop? Is there a way to find out without using Reflector (I have no experience with Reflector and I Don't know C#)?...

Does Ruby perform Tail Call Optimization?

Functional languages lead to use of recursion to solve a lot of problems, and therefore many of them perform Tail Call Optimization (TCO). TCO causes calls to a function from another function (or itself, in which case this feature is also known as Tail Recursion Elimination, which is a subset of TCO), as the last step of that function, t...

Limit to the recursion depth in tail recursion in languages implementing TCO?

What is the theoretical/practical limit to the recursion depth in languages implementing Tail Call optimisation? (Please assume that the recurring function is properly tail call-ed). My guess is that the theoretical limit is NONE, as there is no recursive process, even though it is recursive procedure. Practical limit would be that allo...

Doubt regarding a tail optimized code under 'gdb'

Consider a tail recursive factorial implementation in C: #include <stdio.h> unsigned long long factorial(unsigned long long fact_so_far, unsigned long long count, unsigned long long max_count){ if (max_count==0 || max_count==1 || count >= max_count) return fact_so_far; else { printf("%llu %p \n", count, &factorial); ...

Explain to me what the big deal with tail call optimization is and why Python needs it

So apparently, there's been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he didn't "get it." I'm in the same boat as Guido. I understand the concept of tail call optimization. I just can't think of any reason why Python really needs it. ...

Do recursive sequences leak memory?

I like to define sequences recursively as follows: let rec startFrom x = seq { yield x; yield! startFrom (x + 1) } I'm not sure if recursive sequences like this should be used in practice. The yield! appears to be tail recursive, but I'm not 100% sure since its being called from inside another IEnumerable. From...

Does using a lot of tail-recursion in Erlang slow it down?

I've been reading about Erlang lately and how tail-recursion is so heavily used, due to the difficulty of using iterative loops. Doesn't this high use of recursion slow it down, what with all the function calls and the effect they have on the stack? Or does the tail recursion negate most of this? ...

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 PL/SQL. Thanks in advance. ...

What is tail-recursion elimination?

Steve Yegge mentioned it in a blog post and I have no idea what it means, could someone fill me in? Is it the same thing as tail call optimization? ...

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

A friend gave me this code snippet in Closure (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 code I've written looks like this: def from(n: Int): Stream[Int] = Stream.cons(n, from(n+...

is erlangs recursive functions not just a goto?

Hi, Just to get it straight in my head. Consider this example bit of Erlang code: test() -> receive {From, whatever} -> %% do something test(); {From, somethingelse} -> %% do something else test(); end. Isn't the test() call, just a goto? I ask ...