Pascal's triangle: Difference between revisions

→‎{{header|Perl 6}}: translation of perl (5) code
(→‎{{header|Perl 6}}: translation of perl (5) code)
Line 758:
 
say .perl for pascal 10;</lang>
 
{{trans|Perl}}
<lang perl6>
use v6 ;
sub pascal ($n)
# Prints out $n rows of Pascal's triangle.
{$n < 1 and return Mu;
say "1";
$n == 1 and return 1;
my @last = [1];
for (1 .. $n - 1) -> $row
{my @this = map (-> $a {@last[$a] + @last[$a + 1]}), 0 .. $row - 2;
@last = (1, @this, 1);
say join(' ', @last) ;}
return 1;}
</lang>
 
The following routine returns a lazy list of lines using the series operator. With a lazy result you need not tell the routine how many you want; you can just use a slice subscript to get the first N lines:
<lang perl6>sub pascal { [1], -> @p { [0, @p Z+ @p, 0] } ... * }
Anonymous user