haskell-lectures/VL1/content/VL1_ADT5.tex

23 lines
1020 B
TeX
Raw Normal View History

Let's define something more complex. How about a tree?
\pause
\begin{haskellcode}
data Tree = Leaf Char
| Node Tree Int Tree
\end{haskellcode}
Uh... that looks mean. Let's examine this.\\
\pause
We have:
\begin{itemizep}
\item defined a data type \code{Tree}
\item a constructor \code{Leaf} of type \code{Tree} with one arguments of type \code{Char}
\item a constructor \code{Node} of type \code{Tree} with 3 arguments
\begin{itemizep}
\item \code{Tree}
\item \code{Int}
\item \code{Tree}
\end{itemizep}
\end{itemizep}
\slidep
That means: a \code{Tree} can either be a \code{Leaf} or an internal \code{Node} with two sub-trees. If we want to create a \code{Leaf}, we have to pass the constructor a \code{Char}. If we want to create a \code{Node}, we have to pass 3 arguments, in order: another \code{Tree}, an \code{Int} and yet another \code{Tree}.\\
So we can save information in the leafs (\code{Char}) and in the internal nodes (\code{Int}).\\
This is just an example. There are endless more ways of trees.