On Jul 30, 2011, at 8:30 AM, Mark J. Reed wrote:
--snip--
Does Perl6's variety of
laziness support this sort of definition?
It's not an infinite list; zipWith stops zipping as soon as either
list is
empty. But the self-reference in the definition means it still has
to be
computed lazily.
Yes, Perl 6 does support laziness in this sort of definition, via "run-
time binding".
I was delighted to find that it already works in Rakudo!
(We also support Haskell's cool pictogram-style declarations.)
Here is a quick conversion of the Haskell solution into Perl 6; its
output
(when run in Rakudo) exactly matches the output of my iterative Perl 6
solution (http://rosettacode.org/mw/index.php/Gray_code#Perl_6):
our multi sub infix:<xor2> ( $x, $y ) { ( $x + $y ) % 2 };
multi bin_to_gray ( [] ) { [] }
multi bin_to_gray ( [$head, *@tail] ) {
return [ $head, ( @tail Zxor2 ($head, @tail) ) ];
}
multi gray_to_bin ( [] ) { [] }
multi gray_to_bin ( [$head, *@tail] ) {
my @bin := $head, (@tail Zxor2 @bin); # Note the recursive
definition via bound array
return @bin.flat;
}
for ^32 -> $n {
my @b = $n.fmt('%b').comb;
my $g = bin_to_gray( @b );
my $d = gray_to_bin( $g );
printf "%2d: %5b => %5s => %5s: %2d\n", $n, $n, $g.join, $d.join, :
2($d.join);
die if :2($d.join) != $n;
}
--
Hope this helps,
Bruce Gray (Util of PerlMonks)