Algebraic datatype

From HaskellWiki
Revision as of 12:56, 31 May 2007 by Lemming (talk | contribs) (taken from Hawiki)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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:

data Tree a = Leaf a | Branch (Tree a) (Tree a)

How would this describe a tree like this:

   5
  / \
 4   7


Branch (Branch (Leaf 5) (Leaf 4)) (Leaf 7)

Similar to Lisp's ((5 4) 7) (or maybe ((5 . 4) . 7)).

data Tree a = Leaf | Branch (Tree a) a (Tree a)
Branch (Branch Leaf 4 Leaf) 5 (Branch Leaf 7 Leaf)

Leaf can handle empty trees.


data Tree a = Leaf a | Branch a (Tree a) (Tree a)

and

data Tree a = Leaf a | Branch a [Tree a]

handle internally-labelled binary, and rose trees.