From: Louis-Philippe Date: 2011-04-19T05:40:07+09:00 Subject: Re: Tail Call Optimization (Tail Recursion) --000e0ce049e488182b04a1376506 Content-Type: text/plain; charset=ISO-8859-1 it seems like fib(50000) is not only too big for MRI ruby, but also Python and Lua and Rubinius panic on it... jRuby also complains but points to instructions to increase the stack... otherways, I have MacRuby, Racket and Haskell in their tail recursive fib solving routine, no results yet but all are still working... 2011/4/18 WJ > 7stud -- wrote: > > > Terry Michaels wrote in post #993440: > > > I did some googling to find out if Ruby supports tail call > optimization, > > > as I wanted to write tail-recursive functions. All posts I found > > > indicated that 1.9 supports this, but it is not enabled by default. > > > However, all those posts were about two years old, so I'm wondering > what > > > is the current situation: do I still need to upgrade to 1.9 to get that > > > capability? Or is there something in 1.8 I can enable? > > > > Couldn't you test that by creating two recursive functions: one with a > > tail call and one without, and then benchmarking them? > > def fib n, a = 0, b = 1 > if n == 0 > b > else > a, b = b, a + b > fib n - 1, a, b > end > end > > 1.upto(9){|n| > p [n, fib(n)] > } > > p fib( 50_000 ) > > ==> > > [1, 1] > [2, 2] > [3, 3] > [4, 5] > [5, 8] > [6, 13] > [7, 21] > [8, 34] > [9, 55] > try4.rb:23:in `fib': stack level too deep (SystemStackError) > from try4.rb:23:in `fib' > from try4.rb:31 > > > irb(main):004:0> VERSION > => "1.8.7" > > --000e0ce049e488182b04a1376506--