Hier folgt die Zusammenfassung der zweiten Hälfte des dritten Kapitels von "Real World Haskell".
data Maybe a = Just a
| Nothing
data List a = Cons a (List a)
| Nil
data Tree a = Node a (Tree a) (Tree a)
| Empty
deriving (Show)
mySecond :: [a] -> a
mySecond xs = if null (tail xs)
then error “list too short”
else head (tail xs)
:type error
error :: [Char] -> a
safeSecond :: [a] -> Maybe a
safeSecond [] = Nothing
safeSecond xs = if null (tail xs)
then Nothing
else Just (head (tail xs))
tidySecond :: [a] -> Maybe a
tidySecond (_:x:_) = Just x
tidySecond _ = Nothing
lend amount balance = let reserve = 100
newBalance = balance - amount
in if balance < reserve
then Nothing
else Just newBalance
foo = let a = 1
in let b = 2
in a + b
bar = let x = 1
in ((let x = “foo” in x), x)
lend2 amount balance = if amount < reserve * 0.5
then Just newBalance
else Nothing
where reserve = 100
newBalance = balance - amount
pluralise :: String -> [Int] -> [String]
pluralise word counts = map plural counts
where plural 0 = “no “ ++ word ++ “s”
plural 1 = “one “ ++ word
plural n = show n ++ “ “ ++ word ++ “s”
itemName = “foobar”
Indentation
Guards