Pascal's triangle: Difference between revisions

Content added Content deleted
(→‎one-liner: add lazy constant form)
Line 1,226: Line 1,226:


See http://perlgeek.de/blog-en/perl-6/pascal-triangle.writeback for a partial explanation of how this one-liner example works.
See http://perlgeek.de/blog-en/perl-6/pascal-triangle.writeback for a partial explanation of how this one-liner example works.

One problem with the routine above is that it might recalculate the sequence each time you call it. Slightly more idiomatic would be to define the sequence as a lazy constant, which gives the compiler more latitude in whether to cache or recalculate intermediate values. (A desperate garbage collector can trim an overly long lazy constant, since it can always be recreated later, assuming the programmer didn't lie about its constancy.) In that sense, this is a better translation of the Haskell too.

{{works with|niecza|2011.07}}
<lang perl6>constant Pascal = [1], -> @p { [0, @p Z+ @p, 0] } ... *;

.say for Pascal[^10];


=={{header|PHP}}==
=={{header|PHP}}==