Same fringe: Difference between revisions
Content added Content deleted
(added Clojure version) |
(→{{header|Perl 6}}: putting gather in the proto) |
||
Line 1,149: | Line 1,149: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
{{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. |
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> |
<lang perl6>proto fringe($) { gather {*} } |
||
multi fringe(Pair $node) { fringe $_ for $node.kv } |
|||
multi fringe(Any $leaf) { take $leaf } |
|||
gather fringey $tree; |
|||
} |
|||
sub samefringe ($a, $b) { fringe($a) eqv fringe($b) }</lang> |
sub samefringe ($a, $b) { fringe($a) eqv fringe($b) }</lang> |
||
Testing: |
Testing: |