haskell-lectures/VL1/content/VL1_ADT5.tex

23 lines
1.1 KiB
TeX

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 \hinline{Tree}
\item a constructor \hinline{Leaf} of type \hinline{Tree} with one arguments of type \hinline{Char}
\item a constructor \hinline{Node} of type \hinline{Tree} with 3 arguments
\begin{itemizep}
\item \hinline{Tree}
\item \hinline{Int}
\item \hinline{Tree}
\end{itemizep}
\end{itemizep}
\slidep
That means: a \hinline{Tree} can either be a \hinline{Leaf} or an internal \hinline{Node} with two sub-trees. If we want to create a \hinline{Leaf}, we have to pass the constructor a \hinline{Char}. If we want to create a \hinline{Node}, we have to pass 3 arguments, in order: another \hinline{Tree}, an \hinline{Int} and yet another \hinline{Tree}.\\
So we can save information in the leafs (\hinline{Char}) and in the internal nodes (\hinline{Int}).\\
This is just an example. There are endless more ways of trees.