Same fringe: Difference between revisions

→‎{{header|Perl 6}}: oops, forgot to remove one line
(→‎{{header|Perl 6}}: the gather {*} thing does not work anymore for some reason)
(→‎{{header|Perl 6}}: oops, forgot to remove one line)
Line 1,151:
{{Works with|Rakudo|#67 ("Bicycle")}}
Unlike in Perl 5, where <tt>=></tt> is just a synonym for comma, in Perl 6 it creates a true Pair object. So here we use Pair objects for our "cons" cells, just as if we were doing this in Lisp. We use the <tt>gather/take</tt> construct to harvest the leaves lazily as the elements are visited with a standard recursive algorithm, using multiple dispatch to differentiate nodes from leaves. The <tt>eqv</tt> value equivalence is applied to the two lists in parallel.
<lang perl6>sub samefringefringe ($a, $btree) { fringe($a) eqv fringe($b) }
sub fringe ($tree) {
multi sub fringey (Pair $node) { fringey $_ for $node.kv; }
multi sub fringey ( Any $leaf) { take $leaf; }
1,934

edits