From: Logan Capaldo Date: 2005-12-19T15:31:44+09:00 Subject: Re: ruby beats them all On Dec 15, 2005, at 8:46 PM, Matt O'Connor wrote: > jwesley wrote: >> If Ruby properly handled tail-recursion, then the "accumulator >> passing" >> style work for any number: >> def fib n >> fib_helper( n, 1, 1) >> end >> def fib_helper n, next_val, val >> n < 1 ? val : fib_helper( n-1, next_val + val, next_val) >> end >> the above code (in accumulator-passing style) only works through >> about >> n=1300 for me. > > Though a more "functional approach" is to hide the helper function: > > def fib n > def fib_helper n, next_val, val > n < 1 ? val : fib_helper( n-1, next_val + val, next_val) > end > fib_helper( n, 1, 1) > end > > > Matt > Sadly, that doesn't really hide the helper function. fib_helper will be at the same scope as fib, Ruby doesn't currently do nested function definitions.