in reply to Pure Perl tail call optimization
pdcawley,
Eager to try my tail recursion, I tried your example and was bummed when it didn't work. I figured out that to make it work you had to do something like:
Eager to try my tail recursion, I tried your example and was bummed when it didn't work. I figured out that to make it work you had to do something like:
Where you had to supply an initial total for it to work. Here is a slightly modified version.print tail_factorial(1, 8);
To verify that it works I tested it for a value of 150 - no deep recursion errors - yeah. Thanks for the p6 weekly summaries too.sub tail_factorial { my ($n, $total) = @_; $total = 1 if ! $total; return $total if ! $n; @_ = ($n - 1, $total * $n); goto &tail_factorial; }
Cheers - L~R
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Pure Perl tail call optimization
by demerphq (Chancellor) on Mar 09, 2004 at 19:48 UTC | |
by Limbic~Region (Chancellor) on Mar 09, 2004 at 19:51 UTC | |
by demerphq (Chancellor) on Mar 10, 2004 at 08:48 UTC | |
by tilly (Archbishop) on Mar 11, 2004 at 03:14 UTC | |
by pdcawley (Hermit) on Mar 19, 2004 at 21:19 UTC |
In Section
Meditations