Algebraic data types: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Merge omitted languages at bottom and add Processing)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 306:
((tree t a y b) (tree 'black a y b))))</lang>
 
=={{Headerheader|E}}==
{{trans|Haskell}}
 
Line 1,193:
in T (B,a,y,b)
</lang>
 
 
=={{header|Oz}}==
Line 1,225 ⟶ 1,224:
t(b A Y B)
end</lang>
 
=={{header|Perl}}==
{{works with|Perl|5.010}}
Line 1,314:
Tree: <B,<B,<B,<R,_,1,_>,2,_>,3,<B,_,4,_>>,5,<B,<B,_,6,_>,7,<B,<R,_,8,_>,9,<R,_,10,_>>>>.
Done</pre>
 
=={{header|Perl 6}}==
{{works with|rakudo|2016.11}}
Perl 6 doesn't have algebraic data types (yet), but it does have pretty good pattern matching in multi signatures.
<lang perl6>enum RedBlack <R B>;
 
multi balance(B,[R,[R,$a,$x,$b],$y,$c],$z,$d) { [R,[B,$a,$x,$b],$y,[B,$c,$z,$d]] }
multi balance(B,[R,$a,$x,[R,$b,$y,$c]],$z,$d) { [R,[B,$a,$x,$b],$y,[B,$c,$z,$d]] }
multi balance(B,$a,$x,[R,[R,$b,$y,$c],$z,$d]) { [R,[B,$a,$x,$b],$y,[B,$c,$z,$d]] }
multi balance(B,$a,$x,[R,$b,$y,[R,$c,$z,$d]]) { [R,[B,$a,$x,$b],$y,[B,$c,$z,$d]] }
 
multi balance($col, $a, $x, $b) { [$col, $a, $x, $b] }
multi ins( $x, @s [$col, $a, $y, $b] ) {
when $x before $y { balance $col, ins($x, $a), $y, $b }
when $x after $y { balance $col, $a, $y, ins($x, $b) }
default { @s }
multi ins( $x, Any:U ) { [R, Any, $x, Any] }
 
multi insert( $x, $s ) {
[B, |ins($x,$s)[1..3]];
 
sub MAIN {
my $t = Any;
$t = insert($_, $t) for (1..10).pick(*);
say $t.gist;
}</lang>
This code uses generic comparison operators <tt>before</tt> and <tt>after</tt>, so it should work on any ordered type.
{{out}}
<pre>[B [B [B (Any) 1 [R (Any) 2 (Any)]] 3 [B (Any) 4 [R (Any) 5 (Any)]]] 6 [B [B (Any) 7 (Any)] 8 [B [R (Any) 9 (Any)] 10 (Any)]]]</pre>
 
=={{header|Phix}}==
Line 1,566 ⟶ 1,534:
\-15[R]
</pre>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
{{works with|rakudo|2016.11}}
Perl 6 doesn't have algebraic data types (yet), but it does have pretty good pattern matching in multi signatures.
<lang perl6>enum RedBlack <R B>;
 
multi balance(B,[R,[R,$a,$x,$b],$y,$c],$z,$d) { [R,[B,$a,$x,$b],$y,[B,$c,$z,$d]] }
multi balance(B,[R,$a,$x,[R,$b,$y,$c]],$z,$d) { [R,[B,$a,$x,$b],$y,[B,$c,$z,$d]] }
multi balance(B,$a,$x,[R,[R,$b,$y,$c],$z,$d]) { [R,[B,$a,$x,$b],$y,[B,$c,$z,$d]] }
multi balance(B,$a,$x,[R,$b,$y,[R,$c,$z,$d]]) { [R,[B,$a,$x,$b],$y,[B,$c,$z,$d]] }
 
multi balance($col, $a, $x, $b) { [$col, $a, $x, $b] }
multi ins( $x, @s [$col, $a, $y, $b] ) {
when $x before $y { balance $col, ins($x, $a), $y, $b }
when $x after $y { balance $col, $a, $y, ins($x, $b) }
default { @s }
multi ins( $x, Any:U ) { [R, Any, $x, Any] }
 
multi insert( $x, $s ) {
[B, |ins($x,$s)[1..3]];
 
sub MAIN {
my $t = Any;
$t = insert($_, $t) for (1..10).pick(*);
say $t.gist;
}</lang>
This code uses generic comparison operators <tt>before</tt> and <tt>after</tt>, so it should work on any ordered type.
{{out}}
<pre>[B [B [B (Any) 1 [R (Any) 2 (Any)]] 3 [B (Any) 4 [R (Any) 5 (Any)]]] 6 [B [B (Any) 7 (Any)] 8 [B [R (Any) 9 (Any)] 10 (Any)]]]</pre>
 
=={{header|Rascal}}==
10,327

edits