in reply to Re: (ichimunki) Re: Ackerman vs. Perl : KO in 3 rounds
in thread Ackerman vs. Perl : KO in 3 rounds

But this routine sets up an infinite loop as far as I can tell. When I added a quick print "$_[0],$_[1]" to the beginning of the sub, the output led me to believe the problem here is the algorithm itself, not the technique used to save stack space. Am I missing something?

Replies are listed 'Best First'.
Reeee: Ackerman vs. Perl : KO in 3 rounds
by John M. Dlugosz (Monsignor) on Dec 18, 2001 at 03:30 UTC
    Could be. I think the first recursive call messes up the left argument, since the stack trick basically makes them "static" variables. So that technique doesn't work for multiple recursive calls...owell. That's just one of the sinister things about this function. For the same reason, it won't "tail-end" optomize out the recursion.
    sub A { return 0 if $_[1] == 0; return 2*$_[1] if $_[0] == 0; return 2 if $_[1] == 1; my $mm= $_[0]-1; --$_[1]; $_[1]= &A; $_[0]= $mm; return &A; }
    That gives the right answer for A(3,3), but blows up with reading from a bad memory location on A(4,3).