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

Does ES6 Tail Call Optimization Cover Generators?

Does ES6's support for tail call optimization cover tail calls in generators? Suppose I have this generator for integers >= 0: var nums = function* (n) { n = n || 0; yield n; yield* nums(n + 1); }; Currently, in Chrome and Firefox, it…
andyk
  • 1,628
  • 15
  • 12
10
votes
1 answer

(How) can I make this monadic bind tail-recursive?

I have this monad called Desync - [] module DesyncModule = /// The Desync monad. Allows the user to define in a sequential style an operation that spans /// across a bounded number of events. Span is bounded because I've yet to…
Bryan Edds
  • 1,696
  • 12
  • 28
10
votes
1 answer

Why doesn't year=year+1 fail with stack overflow?

year.hs: year = year + 1 main = print year This is not a tail recursive call: year = year + 1 year = (year + 1) + 1 year = ((year + 1) + 1) + 1 ... However runhaskell year.hs does not output anything, which indicates that it run into infinite…
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
10
votes
1 answer

How do you make this Haskell power function tail recursive?

How would I make this Haskell power function tail recursive? turboPower a 0 = 1 turboPower a b | even b = turboPower (a*a) (b `div` 2) | otherwise = a * turboPower a (b-1)
Linda Cohen
  • 217
  • 2
  • 9
10
votes
3 answers

Tail Recursion optimization for JavaScript?

My apologies to everyone for previous versions of this being vague. Someone has decided to have pity on the new girl and help me rewrite this question - here's an update that I hope will clear things up (and, thanks to all those who have been so…
user3606304
10
votes
3 answers

F# has tail call elimination?

In this talk, in the first 8 minutes, Runar explains that Scala has problems with tail call elimination, this makes me wonder whether F# has similar problems ? If not, why not ?
jhegedus
  • 20,244
  • 16
  • 99
  • 167
10
votes
2 answers

Scala, tail recursion vs. non tail recursion, why is tail recursion slower?

I was explaining a friend that I expected non tail recursive function in Scala to be slower than tail recursive ones, so I decided to verify it. I wrote a good old factorial function both ways and attempted to compare the results. Here's the…
marc-antoine
  • 145
  • 9
10
votes
1 answer

Tail recursion in clojure

This is a lisp code that uses tail recursion. (defun factorial (f n) (if (= n 1) f (factorial (* f n) (- n 1)))) I translate this into clojure code expecting the same tail recursion optimization. (defn fact [f n] (if (= n…
prosseek
  • 182,215
  • 215
  • 566
  • 871
10
votes
4 answers

Stack overflow from recursive function call in Lisp

I am learning Lisp from the book "The Land of Lisp" by Conrad Barski. Now I have hit my first stumbling block, where the author says: Calling yourself in this way is not only allowed in Lisp, but is often strongly encouraged after showing the…
mydoghasworms
  • 18,233
  • 11
  • 61
  • 95
10
votes
3 answers

Scala recursion vs loop: performance and runtime considerations

I've wrote a naïve test-bed to measure the performance of three kinds of factorial implementation: loop based, non tail-recursive and tail-recursive. Surprisingly to me the worst performant was the loop ones («while» was expected to be more…
Lord of the Goo
  • 1,214
  • 15
  • 31
10
votes
3 answers

iPhone dev -- performSelector:withObject:afterDelay or NSTimer?

To repeat a method call (or message send, I guess the appropriate term is) every x seconds, is it better to use an NSTimer (NSTimer's scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:) or to have the method recursively call itself at…
mk12
  • 25,873
  • 32
  • 98
  • 137
10
votes
9 answers

Is Erlang's recursive functions not just a goto?

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…
Toad
  • 15,593
  • 16
  • 82
  • 128
10
votes
2 answers

Does tail recursion necessarily need an accumulator?

For instance, since the following function don't have an accumulator, is it still tail recursive? belong:: (Ord a) => a -> [a] -> Bool belong a [] = False belong a (h:t) | a == h = True | otherwise = belong a t All the computations in the…
user1493813
  • 981
  • 1
  • 8
  • 16
10
votes
1 answer

Tail recursive functions in Scheme

I'm studying for a Christmas test and doing some sample exam questions, I've come across this one that has me a bit stumped I can do regular recursion fine, but I can't wrap my head around how to write the same thing using tail recursion. Regular…
Eogcloud
  • 1,335
  • 4
  • 19
  • 44
10
votes
3 answers

loop through two variable in Haskell

What is the haskell way to do this? for (int i = 0 ; i < 1000 ; i++) for (int j = 0 ; j < 1000 ; j++) ret = foo(i , j ) #I need the return value. More background: I am solving euler problem 27 , and I have got: …
pierrotlefou
  • 39,805
  • 37
  • 135
  • 175