Algebraic data types: Difference between revisions

Content added Content deleted
No edit summary
Line 8: Line 8:
{{trans|Haskell}}
{{trans|Haskell}}


In E, a pattern can be used almost anywhere a variable name can. Additionally, there are two operators used for pattern matching idioms: <code>=~</code> (returns success as a boolean, somewhat like Perl's <code>=~</code>), and <code>switch</code> (matches multiple patterns, like Haskell's <code>case</code>).
In E, a pattern can be used almost anywhere a variable name can. Additionally, there are two operators used for pattern matching idioms: <tt>=~</tt> (returns success as a boolean, somewhat like Perl's <tt>=~</tt>), and <tt>switch</tt> (matches multiple patterns, like Haskell's <tt>case</tt>).


Both of those operators are defined in terms of the basic bind/match operation: <code>def <var>pattern</var> exit <var>failure_handler</var> := <var>specimen</var></code>
Both of those operators are defined in terms of the basic bind/match operation: <tt>def <var>pattern</var> exit <var>failure_handler</var> := <var>specimen</var></tt>


def balance(tree) {
def balance(tree) {
Line 51: Line 51:
=={{header|Haskell}}==
=={{header|Haskell}}==


<code lang="haskell">
<lang lang="haskell">
data Color = R | B
data Color = R | B
data Tree a = E | T Color (Tree a) a (Tree a)
data Tree a = E | T Color (Tree a) a (Tree a)
Line 70: Line 70:
| otherwise = s
| otherwise = s
T _ a y b = ins s
T _ a y b = ins s
</code>
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==