Difference between revisions of "Algebraic datatype"

From HaskellWiki
Jump to navigation Jump to search
(taken from Hawiki)
 
m
 
(One intermediate revision by one other user not shown)
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.
 
   
  +
[[Category: Pages to be removed]]
== 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]]
 

Latest revision as of 21:00, 7 June 2023