@@ -21,27 +21,45 @@ numbers).
22data Maybe a = Just a | Nothing
22data Maybe a = Just a | Nothing
24−{-| Apply a function to the contents of a `Maybe`.
24+{-| Provide a default value and a function to extract the contents of a `Maybe`.
25−Return default when given `Nothing`.
25+When given `Nothing` you get the default, when given a `Just` you apply the
26+function to the associated value.
28+ isPositive : Maybe Int -> Bool
29+ isPositive maybeInt = maybe False (\n -> n > 0) maybeInt
31+ map : (a -> b) -> Maybe a -> Maybe b
32+ map f m = maybe Nothing (\x -> Just (f x)) m
27maybe : b -> (a -> b) -> Maybe a -> b
34maybe : b -> (a -> b) -> Maybe a -> b
28maybe b f m = case m of
35maybe b f m = case m of
29 Just v -> f v
36 Just v -> f v
30 Nothing -> b
37 Nothing -> b
32−{-| Check if constructed with `Just`.
39+{-| Check if a maybe happens to be a `Just`.
41+ isJust (Just 42) == True
42+ isJust (Just []) == True
43+ isJust Nothing == False
34isJust : Maybe a -> Bool
45isJust : Maybe a -> Bool
35isJust = maybe False (\_ -> True)
46isJust = maybe False (\_ -> True)
37{-| Check if constructed with `Nothing`.
48{-| Check if constructed with `Nothing`.
50+ isNothing (Just 42) == False
51+ isNothing (Just []) == False
52+ isNothing Nothing == True
39isNothing : Maybe a -> Bool
54isNothing : Maybe a -> Bool
40isNothing = not . isJust
55isNothing = not . isJust
57+cons : Maybe a -> [a] -> [a]
42cons mx xs = maybe xs (\x -> x :: xs) mx
58cons mx xs = maybe xs (\x -> x :: xs) mx
44{-| Filters out Nothings and extracts the remaining values.
60{-| Filters out Nothings and extracts the remaining values.
62+ justs [Just 0, Nothing, Just 5, Just 7] == [0,5,7]
46justs : [Maybe a] -> [a]
64justs : [Maybe a] -> [a]
47justs = foldr cons []
65justs = foldr cons []