Same fringe: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Combine into a single file for ease of testing) |
|||
Line 1,489: | Line 1,489: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
{{ |
{{works with|Rakudo|2018.03}} |
||
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>sub fringe ($tree) { |
<lang perl6>sub fringe ($tree) { |
||
Line 1,498: | Line 1,498: | ||
} |
} |
||
sub samefringe ($a, $b) { fringe($a) eqv fringe($b) } |
sub samefringe ($a, $b) { fringe($a) eqv fringe($b) } |
||
Testing: |
# Testing: |
||
⚫ | |||
⚫ | |||
my $b = 1 => (( 2 => 3 ) => (4 => (5 => ((6 => 7) => 8)))); |
my $b = 1 => (( 2 => 3 ) => (4 => (5 => ((6 => 7) => 8)))); |
||
my $c = (((1 => 2) => 3) => 4) => 5 => 6 => 7 => 8; |
my $c = (((1 => 2) => 3) => 4) => 5 => 6 => 7 => 8; |