Hier folgt die Zusammenfassung der zweiten Hälfte des dritten Kapitels von "Real World Haskell".
data Maybe a = Just a
| Nothing
- a is a type variable (like Java generics)
```haskell
data List a = Cons a (List a)
| Nil
- Nil: simples value of type List, may be used as parameter for Cons
```haskell
data Tree a = Node a (Tree a) (Tree a)
| Empty
deriving (Show)
```
- binary tree is either a node with two children or an empty value
- children are themselves binary trees
# Reporting Errors
- standard function: error :: String -> a
- can be called if something went wrong
```haskell
mySecond :: [a] -> a
mySecond xs = if null (tail xs)
then error “list too short”
else head (tail xs)
```
```haskell
:type error
error :: [Char] -> a
## Reporting Errors with Maybe
safeSecond :: [a] -> Maybe a
safeSecond [] = Nothing
safeSecond xs = if null (tail xs)
then Nothing
else Just (head (tail xs))
- caller can decide what to do with nullable
tidySecond :: [a] -> Maybe a
tidySecond (_:x:_) = Just x
tidySecond _ = Nothing