Man or boy test
luben karavelov
luben at unixsol.org
Thu Nov 6 23:12:01 UTC 2008
François Perrad wrote:
> Do you know the Knuth's "man or boy" test ?
>
> If not, see http://en.wikipedia.org/wiki/Man_or_boy_test
> that contains explanations and implementations in various languages.
>
> A Perl5 implementation is :
> sub A {
> my ($k, $x1, $x2, $x3, $x4, $x5) = @_;
> my $B;
> $B = sub {
> return A(--$k, $B, $x1, $x2, $x3, $x4);
> };
> return &$x4() + &$x5() if ($k <= 0);
> return &$B();
> }
>
> print A(10, sub{1}, sub{-1}, sub{-1}, sub{1}, sub{0}), "\n";
> # the expected result is -67
>
> In r32377, I commit a Lua implementation that works on Parrot.
>
> Can we do a Perl6 implementation with the current Rakudo ?
>
> François.
>
> _______________________________________________
> http://lists.parrot.org/mailman/listinfo/parrot-dev
Here it is, straight translation of the perl5 code:
sub A($k, $x1, $x2, $x3, $x4, $x5) {
my $B = sub { A(--$k, $B, $x1, $x2, $x3, $x4) };
$k <= 0 ?? $x4() + $x5() !! $B();
}
say A(10, sub{1}, sub{-1}, sub{-1}, sub{1}, sub{0} );
But it reaches max recursion limit for $k>3
luben
More information about the parrot-dev
mailing list