Examples
One of the most common uses of algebraic data types is to represent singly linked lists. A list type is a sum type with two variants, Nil
for an empty list and Cons x xs
for the combination of a new element x with a list xs to create a new list:
Cons
is an abbreviation of construct. Many languages have special syntax for lists. For example, Haskell and ML use for
Nil
, ::
or :
for Cons
, and square brackets for entire lists. So Cons 1 (Cons 2 (Cons 3 Nil))
would normally be written as 1:2:3:
or in Haskell, or as
1::2::3::
or in ML.
For another example, in Haskell we can define a new algebraic data type, Tree
:
Here, Empty
represents an empty tree, Leaf
contains a piece of data, and Node
organizes the data into branches.
In most languages that support algebraic data types, it is possible to define parametric types. Examples are given later in this article.
Somewhat similar to a function, a data constructor is applied to arguments of an appropriate type, yielding an instance of the data type to which the type constructor belongs. For instance, the data constructor Leaf
is logically a function Int -> Tree
, meaning that giving an integer as an argument to Leaf
produces a value of the type Tree
. As Node
takes two arguments of the type Tree
itself, the datatype is recursive.
Operations on algebraic data types can be defined by using pattern matching to retrieve the arguments. For example, consider a function to find the depth of a Tree
, given here in Haskell:
Thus, a Tree
given to depth
can be constructed using any of Empty
, Leaf
or Node
and we must match for any of them respectively to deal with all cases. In case of Node
, the pattern extracts the subtrees l
and r
for further processing.
Algebraic data types are particularly well-suited to the implementation of abstract syntax. For instance, the following algebraic data type describes a simple language representing numerical expressions:
data Expression = Number Int | Add Expression Expression | Minus Expression | Mult Expression Expression | Divide Expression ExpressionAn element of such a data type would have a form such as Mult (Add (Number 4) (Minus (Number 1))) (Number 2)
.
Writing an evaluation function for this language is a simple exercise; however, more complex transformations also become feasible. For instance, an optimization pass in a compiler might be written as a function taking an abstract expression as input and returning an optimized form.
Read more about this topic: Algebraic Data Type
Famous quotes containing the word examples:
“No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.”
—André Breton (18961966)
“Histories are more full of examples of the fidelity of dogs than of friends.”
—Alexander Pope (16881744)
“In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.”
—Michel de Montaigne (15331592)