\begin{slide}{Algebraische Datentypen (1)}

\begin{verbatim}
data Foo = Foo { bar :: Int, baz :: String } 
    deriving Show
x :: Foo
x = Foo { bar = 3, baz = "hal" }
\end{verbatim}

\end{slide}

\begin{slide}{Pattern Matching}

\begin{verbatim}
data T = A { foo :: Int } 
       | B { bar :: String } 
    deriving Show

f :: T -> Int
f x = case x of
    A {} -> foo x
    B {} -> length $ bar x
\end{verbatim}
\bigskip
Bespiel:
\begin{verbatim}
data Bool = False | True
\end{verbatim}

\end{slide}

\begin{slide}{Rekursive Datentypen}

\begin{verbatim}
data Tree = Leaf {}
    | Branch { left :: Tree, key :: Int
             , right :: Tree }
full :: Int -> Tree -- vollst. binärer Baum
full h = if h > 0 
    then Branch { left = full (h-1)
          , key = h, right = full (h-1) }
    else Leaf { }
leaves :: Tree -> Int
leaves t = case t of
    Leaf   {} -> ...
    Branch {} -> 0
\end{verbatim}

\end{slide}

\begin{slide}{Polymorphie}

\begin{verbatim}
data Tree a 
    = Leaf {}
    | Branch { left :: Tree a, key :: a
             , right :: a }

inorder :: Tree a -> [ a ]
inorder t = case t of
    ...
\end{verbatim}

\end{slide}

\begin{slide}{Listen}

eigentlich:
\begin{verbatim}
data List a = Nil {}
    | Cons { head :: a, tail :: List a }
\end{verbatim}
aber aus historischen Gründen 
\begin{verbatim}
data [a] = a : [a] | []
\end{verbatim}
Pattern matching dafür:
\begin{verbatim}
length :: [a] -> Int
length l = case l of
    []     -> 0
    x : xs -> ...
\end{verbatim}
Summe der Elemente einer Liste?
\end{slide}

\begin{slide}{Theorie}

  
  \begin{itemize}
  \item jeder Haskell-\verb|data|-Typ ist:

(mehrsortige) Signatur $=$ Menge von Funktionssymbolen mit Sorten (Typen)

\item die Objekte des Typs sind die korrekt zusammengesetzten Terme,
  die aus diesen Symbolen bestehen

  \end{itemize}





\end{slide}
