@@ -1,12 +1,71 @@
1module prelude.maybe
1module prelude.maybe
3−data Maybe a = Nothing | Just a
7+data Maybe a
11+--------------------------------------------------------------------------------
13+--------------------------------------------------------------------------------
15+isNothing : Maybe a -> Bool
16+isNothing Nothing = True
17+isNothing (Just j) = False
19+isJust : Maybe a -> Bool
20+isJust Nothing = False
21+isJust (Just j) = True
23+--------------------------------------------------------------------------------
25+--------------------------------------------------------------------------------
27+maybeToList : Maybe a -> List a
28+maybeToList Nothing = []
29+maybeToList (Just j) = [j]
31+listToMaybe : List a -> Maybe a
32+listToMaybe [] = Nothing
33+listToMaybe (x::xs) = Just x
35+--------------------------------------------------------------------------------
37+--------------------------------------------------------------------------------
5maybe : |(def : b) -> (a -> b) -> Maybe a -> b
39maybe : |(def : b) -> (a -> b) -> Maybe a -> b
6maybe n j Nothing = n
40maybe n j Nothing = n
7maybe n j (Just x) = j x
41maybe n j (Just x) = j x
43+fromMaybe : |(def: a) -> Maybe a -> a
44+fromMaybe def Nothing = def
45+fromMaybe def (Just j) = j
47+toMaybe : Bool -> a -> Maybe a
48+toMaybe True j = Just j
49+toMaybe False j = Nothing
51+catMaybes : List (Maybe a) -> List a
55+ Nothing => catMaybes xs
56+ Just j => j :: catMaybes xs
58+mapMaybe : (a -> Maybe b) -> List a -> List b
62+ Nothing => mapMaybe f xs
63+ Just j => j :: mapMaybe f xs
65+--------------------------------------------------------------------------------
67+--------------------------------------------------------------------------------
9maybe_bind : Maybe a -> (a -> Maybe b) -> Maybe b
69maybe_bind : Maybe a -> (a -> Maybe b) -> Maybe b
10−maybe_bind Nothing k = Nothing
70+maybe_bind Nothing k = Nothing
11maybe_bind (Just x) k = k x
71maybe_bind (Just x) k = k x