Difference between revisions of "Algebraic datatype"

From HaskellWiki
Jump to navigation Jump to search
(taken from Hawiki)
 
(Changing to redirect - page was converted some time ago.)
Line 1: Line 1:
  +
#redirect [[Algebraic data type]]
An algebraic data type is a data type in wich we specify the shape of each element.
 
 
== Example ==
 
 
This description of trees seems to be missing something:
 
 
<haskell>
 
data Tree a = Leaf a | Branch (Tree a) (Tree a)
 
</haskell>
 
 
How would this describe a tree like this:
 
 
<code>
 
5
 
/ \
 
4 7
 
</code>
 
 
----
 
 
<haskell>
 
Branch (Branch (Leaf 5) (Leaf 4)) (Leaf 7)
 
</haskell>
 
Similar to Lisp's <code>((5 4) 7)</code> (or maybe <code>((5 . 4) . 7)</code>).
 
 
<haskell>
 
data Tree a = Leaf | Branch (Tree a) a (Tree a)
 
Branch (Branch Leaf 4 Leaf) 5 (Branch Leaf 7 Leaf)
 
</haskell>
 
Leaf can handle empty trees.
 
 
----
 
 
<haskell>
 
data Tree a = Leaf a | Branch a (Tree a) (Tree a)
 
</haskell>
 
and
 
<haskell>
 
data Tree a = Leaf a | Branch a [Tree a]
 
</haskell>
 
handle internally-labelled binary, and rose trees.
 
 
 
[[Category:Glossary]]
 

Revision as of 01:40, 5 June 2007