Same fringe: Difference between revisions

m
→‎{{header|Perl 6}}: Combine into a single file for ease of testing
m (→‎{{header|Perl 6}}: Combine into a single file for ease of testing)
Line 1,489:
 
=={{header|Perl 6}}==
{{Worksworks with|Rakudo|#67 ("Bicycle")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.
<lang perl6>sub fringe ($tree) {
Line 1,498:
}
 
sub samefringe ($a, $b) { fringe($a) eqv fringe($b) }</lang>
 
# Testing:
<lang perl6>my $a = 1 => 2 => 3 => 4 => 5 => 6 => 7 => 8;
 
<lang perl6>my $a = 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;
10,333

edits