use warnings; use strict; sub A { return 0 if $_[1] == 0; return 2*$_[1] if $_[0] == 0; return 2 if $_[1] == 1; # figure A(m-1, A(m, n-1)) using in-place parameter list to save stac +k. --$_[1]; $_[1]= &A; --$_[0]; return &A; } print A ($ARGV[0], $ARGV[1]);
Ackerman 3 3 prints a few lines to the effect of "attempt to free unreferenced scaler at..." but the file name is messed up. Then it terminates with an attempted memory write to a protected location of 0x0000013b (i.e. a stray pointer).

Anybody else care to try?

—John

Replies are listed 'Best First'.
Re (tilly) 1: Ackerman vs. Perl : KO in 3 rounds
by tilly (Archbishop) on Dec 18, 2001 at 03:23 UTC
    In 5.005 I get that you are trying to modify a readonly value.

    I think a better implementation of the function (at least it runs properly for me) is:

    sub A { BARE_BLOCK: { if (0 == $_[0]) { return 1 + $_[1]; } @_=($_[0] - 1, $_[1] ? A($_[0], $_[1]-1) : 1); redo BARE_BLOCK; } }
    Note that this really does save stack space. Although, considering what Ackermann's function does, this is (I suspect) a losing battle. :-)
      Nice, using redo to explicitly perform tail-end recursion optomization. I'm wondering if the optimizer can figure that out?

      Hmm, but yours gives the wrong answer for A(3,3). (61 instead of 65536)

      —John

        When Perl calls a recursive function it does a huge amount of work saving state, optimizing the next time you call it, etc. For a sobering comparison, compare the implementation above with the following one where I manage a stack explicitly instead of having a stack of function calls:
        sub A { my ($x, $y) = @_; my @stack = (); while (1) { if (0 == $x) { if (@stack) { $y++; $x = pop(@stack); } else { return 1 + $y; } } else { if (0 == $y) { $x--; $y = 1; } else { push @stack, $x-1; $y--; } } } }
        Impressed?
        Sorry. Perl can't detect tail-end recursion for you. In fact Perl's handling of deep recursion is truly horrible.

        As for whether my answers are right or wrong, they agree with this page...

Re: Ackerman vs. Perl : KO in 3 rounds
by dws (Chancellor) on Dec 18, 2001 at 02:55 UTC
    Anybody else care to try?

    Using 5.6.0 on Win32, I get repeated

    Deep recursion on subroutine "main::A" at ack.pl line 11. Deep recursion on subroutine "main::A" at ack.pl line 13.
    Looking at your code, I distrust what you are doing to save stack space.


    Update: Yup. Add   print "A($_[0],$_[1])\n"; to the top of the function to watch it run off the rails. This is not a correct implementation of the Ackerman function.

Re: Ackerman vs. Perl : KO in 3 rounds
by boo_radley (Parson) on Dec 18, 2001 at 03:38 UTC
    With a lot of debug information :
    use warnings; use strict; sub A { print "\n\nentering, \$_[0] is $_[0]\n"; print "entering, \$_[1] is $_[1]\n"; if ($_[1] == 0) {print "1st -- returning 0\n"; return 0 ;} if ($_[0] == 0) {print "2nd -- returning 2*\$_[1] ($_[1])\n"; retu +rn 2*$_[1] } if ($_[1] == 1) {print "3rd -- returning 2\n"; return 2 }; print "not returning : "; # figure A(m-1, A(m, n-1)) using in-place parameter list to save s +tack --$_[1]; print "\$_[1] is $_[1] "; $_[1]= &A; print "\$_[1] is $_[1] "; --$_[0]; print "\$_[1] is $_[1] "; return &A; } print A ($ARGV[0], $ARGV[1]);
    gives :
    C:\>trash 3 3 entering, $_[0] is 3 entering, $_[1] is 3 not returning : $_[1] is 2 entering, $_[0] is 3 entering, $_[1] is 2 not returning : $_[1] is 1 entering, $_[0] is 3 entering, $_[1] is 1 3rd -- returning 2 $_[1] is 2 $_[1] is 2 entering, $_[0] is 2 entering, $_[1] is 2 not returning : $_[1] is 1 entering, $_[0] is 2 entering, $_[1] is 1 3rd -- returning 2 $_[1] is 2 $_[1] is 2 entering, $_[0] is 1 entering, $_[1] is 2 not returning : $_[1] is 1 entering, $_[0] is 1 entering, $_[1] is 1 3rd -- returning 2 $_[1] is 2 $_[1] is 2 entering, $_[0] is 0 entering, $_[1] is 2 2nd -- returning 2*$_[1] ( $_[1] is 4 $_[1] is 4 entering, $_[0] is -1 entering, $_[1] is 4 not returning : $_[1] is 3 entering, $_[0] is -1 entering, $_[1] is 3 not returning : $_[1] is 2 entering, $_[0] is -1 entering, $_[1] is 2 not returning : $_[1] is 1 entering, $_[0] is -1 entering, $_[1] is 1 3rd -- returning 2 $_[1] is 2 $_[1] is 2 entering, $_[0] is -2 entering, $_[1] is 2 not returning : $_[1] is 1

    so, looking at the issue in a purely symptomatic sense, the infinite recursion can be stopped by changing line 11 to :
    if ($_[0] <= 0) {print "2nd -- returning 2*\$_[1] ($_[1])\n"; retu +rn 2*$_[1] }

    , but I don't know how this affects what this sub does, since it only seems to produce 2 ** ARGV[1].

    what gives? what is this code really about?

    update AHA!
    you appear to be implementing the algorithm incorrectly, if I may be so bold :-)

Re: Ackerman vs. Perl : KO in 3 rounds
by VSarkiss (Monsignor) on Dec 18, 2001 at 03:40 UTC

    The hackery with the short-circuit sub calls is part of the problem. If I rewrite this in a "naive" way:

    sub A { my ($m, $n) = @_; return 0 if $n == 0; return 2*$n if $n > 0 and $m == 0; return 2 if $n == 1 and $m > 0; A($m-1, A($m, $n-1)); } print A ($ARGV[0], $ARGV[1]), "\n";
    I get a reasonable answer with (3,3). However, on (4,4), I get "Not a CODE reference at a.pl line 7" followed by a memory fault. (I'm using 5.6.1 on HP-UX, BTW.)

(ichimunki) Re: Ackerman vs. Perl : KO in 3 rounds
by ichimunki (Priest) on Dec 18, 2001 at 02:53 UTC
    I cannot duplicate this error. I get a print() on unopened filehandle error if I leave the space in between A and $ARGV. I get an Argument "Ackerman" isn't numeric in numeric eq (==) error if I take the space out.

    update: I get the same error as dws if I assume that Ackerman is the script name and just pass '3 3' as my args. update 2: removed somewhat argumentative opening line.
      It's a meditation because I don't need an answer to a specific problem to accomplish some task. Rather, I just thought it was interesting and enlightening.

      The idea was that Perl is different than pure-compiled languages like C, and (1) should not have the same limit to stack size since the Perl stack != the asm stack, and (2) I can use the old & construct to not push arguments in the recursive calls. I had also planned to try it with memoize, to look at the speed/memory difference.

      But it goes boom. I think it's a bug if the perl executable hits a stray pointer, instead of giving a proper "out of memory" message.

      —John

        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?