Algebraic data types: Difference between revisions

Content added Content deleted
(Merge omitted languages at bottom and add Processing)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 306: Line 306:
((tree t a y b) (tree 'black a y b))))</lang>
((tree t a y b) (tree 'black a y b))))</lang>


=={{Header|E}}==
=={{header|E}}==
{{trans|Haskell}}
{{trans|Haskell}}


Line 1,193: Line 1,193:
in T (B,a,y,b)
in T (B,a,y,b)
</lang>
</lang>



=={{header|Oz}}==
=={{header|Oz}}==
Line 1,225: Line 1,224:
t(b A Y B)
t(b A Y B)
end</lang>
end</lang>

=={{header|Perl}}==
=={{header|Perl}}==
{{works with|Perl|5.010}}
{{works with|Perl|5.010}}
Line 1,314: Line 1,314:
Tree: <B,<B,<B,<R,_,1,_>,2,_>,3,<B,_,4,_>>,5,<B,<B,_,6,_>,7,<B,<R,_,8,_>,9,<R,_,10,_>>>>.
Tree: <B,<B,<B,<R,_,1,_>,2,_>,3,<B,_,4,_>>,5,<B,<B,_,6,_>,7,<B,<R,_,8,_>,9,<R,_,10,_>>>>.
Done</pre>
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}}==
=={{header|Phix}}==
Line 1,566: Line 1,534:
\-15[R]
\-15[R]
</pre>
</pre>

=={{header|Raku}}==
(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}}==
=={{header|Rascal}}==