Functional programming revision notes and examples.
double :: Num a => a -> a
double x = x * 2
isEmpty :: [a] -> Bool
isEmpty [] = True
isEmpty _ = False
length' :: [a] -> Int
length' [] = 0
length' (_:xs) = 1 + length' xs
map' :: (a -> b) -> [a] -> [b]
map' f [] = []
map' f (x:xs) = f x : map' f xs
filter' :: (a -> Bool) -> [a] -> [a]
filter' p [] = []
filter' p (x:xs)
| p x = x : filter' p xs
| otherwise = filter' p xs
sum' :: Num a => [a] -> a
sum' xs = foldr (+) 0 xs
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
data Tree a = Empty
| Node (Tree a) a (Tree a)
treeSize :: Tree a -> Int
treeSize Empty = 0
treeSize (Node l _ r) = 1 + treeSize l + treeSize r
[] and x:xs.