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
18
votes
6 answers

Is tail recursion possible if a comparison depends on the return value?

I had a homework assignment that asked for a function that uses direct recursion to find the index of the left-most, lowest, negative integer in an array. Additional requirements were for the parameters of the function to be the array and the size…
Matt
  • 1,513
  • 3
  • 16
  • 32
18
votes
5 answers

While or Tail Recursion in F#, what to use when?

Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the…
Peter
  • 47,963
  • 46
  • 132
  • 181
18
votes
5 answers

Convert recursion to tail recursion

I have a question about how to convert 'recursion' to 'tail recursion'. This is not a homework, just a question that popped up when I tried to polish the recursion theorem from a book on algorithms. I am familiar with the 2 typical examples of using…
xxmouse
  • 183
  • 1
  • 1
  • 4
17
votes
8 answers

Splitting a list of items into two lists of odd and even indexed items

I would like to make a function that accepts a list and returns two lists: the first contains every odd item, and the second contains every even item. For example, given [1;2;4;6;7;9], I would like to return [ [1;4;7] ; [2;6;9] ]. I have written…
Nathron
  • 1,639
  • 3
  • 18
  • 25
17
votes
1 answer

Why is tail recursion optimization faster than normal recursion in Python?

While I understand that tail recursion optimization is non-Pythonic, I came up with a quick hack to a question on here that was deleted as soon as a I was ready to post. With a 1000 stack limit, deep recursion algorithms are not usable in Python. …
Joe
  • 2,407
  • 18
  • 20
17
votes
1 answer

Can/does the (forward) pipe operator prevent tail call optimization?

For a parameter optimization problem at work I wrote a genetic algorithm to find some good settings because a brute-force solution is unfeasible. Unfortunately, when I return in the morning, most of the time I'm presented with a…
primfaktor
  • 2,831
  • 25
  • 34
17
votes
1 answer

Tail Call Elimination in Clojure?

Can somebody rewrite this (plt) Scheme code into Clojure? (define (f n) (printf "(f ~a)~n" n) (g n)) (define (g n) (printf "(g ~a)~n" n) (h n)) (define (h n) (printf "(h ~a)~n" n) (f (+ n 1))) In such a way as to not collapse…
tkf
  • 997
  • 1
  • 7
  • 16
17
votes
1 answer

Preventing StackOverflow in language interpreters

F# as a language is great for writing language interpreters or compilers, however, one thing keeps hitting us where we don't want it: the StackOverflowException. It's well known that an SO-exception cannot be caught and cannot be recovered from. An…
Abel
  • 56,041
  • 24
  • 146
  • 247
16
votes
2 answers

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…
Dave Berk
  • 1,591
  • 2
  • 15
  • 17
16
votes
3 answers

Why does s ++ t not lead to a stack overflow for large s?

I'm wondering why Prelude> head $ reverse $ [1..10000000] ++ [99] 99 does not lead to a stack overflow error. The ++ in the prelude seems straight forward and non-tail-recursive: (++) :: [a] -> [a] -> [a] (++) [] ys = ys (++) (x:xs) ys = x : xs…
martingw
  • 4,153
  • 3
  • 21
  • 26
16
votes
3 answers

Tail recursion on R Statistical Environment

Does R support proper tail recursion and where can I find documentation about this?
15
votes
5 answers

In Clojure, is it possible to combine memoization and tail call optimization?

In clojure, I would like to write a tail-recursive function that memoizes its intermediate results for subsequent calls. [EDIT: this question has been rewritten using gcd as an example instead of factorial.] The memoized gcd (greatest common…
viebel
  • 19,372
  • 10
  • 49
  • 83
14
votes
2 answers

Does MATLAB perform tail call optimization?

I've recently learned Haskell, and am trying to carry the pure functional style over to my other code when possible. An important aspect of this is treating all variables as immutable, i.e. constants. In order to do so, many computations that would…
14
votes
1 answer

Clojure JVM 7/8 improvements

Rich Hickey and others have mentioned that Clojure will not get a significant improvement from the upcoming invokeDynamic planned for JVM 7 or 8, but will see a performance gain from tail recursion. Will tail recursion have any effect on (fn [...]…
Ralph
  • 31,584
  • 38
  • 145
  • 282
14
votes
4 answers

How to recognize what is, and what is not tail recursion?

Sometimes it's simple enough (if the self call is the last statement, it's tail recursion), but there are still cases that confuse me. A professor told me that "if there's no instruction to execute after the self-call, it's tail recursion". How…
fingerprint211b
  • 1,176
  • 12
  • 24