1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
<title>The Haskell 98 Library Report: Maybe Utilities</title>
<body bgcolor="#ffffff"> <i>The Haskell 98 Library Report</i><br> <a href="index.html">top</a> | <a href="list.html">back</a> | <a href="char.html">next</a> | <a href="libindex.html">contents</a> <br><hr>
<a name="sect8"></a>
<h2>8<tt> </tt>Maybe Utilities</h2><p>
<table border=2 cellpadding=3>
<tr><td>
<tt><br>
module Maybe(<br>
isJust, isNothing,<br>
fromJust, fromMaybe, listToMaybe, maybeToList,<br>
catMaybes, mapMaybe,<br>
<br>
-- ...and what the Prelude exports<br>
Maybe(Nothing, Just),<br>
maybe<br>
) where<br>
<br>
isJust, isNothing :: Maybe a -> Bool<br>
fromJust :: Maybe a -> a<br>
fromMaybe :: a -> Maybe a -> a<br>
listToMaybe :: [a] -> Maybe a<br>
maybeToList :: Maybe a -> [a]<br>
catMaybes :: [Maybe a] -> [a]<br>
mapMaybe :: (a -> Maybe b) -> [a] -> [b]<br>
</tt></td></tr></table>
<p>
The type constructor <tt>Maybe</tt> is defined in <tt>Prelude</tt> as
<tt><br>
<br>
data Maybe a = Nothing | Just a<br>
<br>
</tt>The purpose of the <tt>Maybe</tt> type is to provide a method of dealing with
illegal or optional values without terminating the program, as would
happen if <tt>error</tt> were used, and without using <tt>IOError</tt> from the <tt>IO
</tt>monad, which would cause the expression to become monadic. A correct
result is encapsulated by wrapping it in <tt>Just</tt>; an incorrect result
is returned as <tt>Nothing</tt>.<p>
Other operations on <tt>Maybe</tt> are provided as part of the monadic
classes in the Prelude.<a name="Maybe"></a><p>
<a name="sect8.1"></a>
<h3>8.1<tt> </tt>Library <tt>Maybe</tt></h3>
<tt><br>
module Maybe(<br>
isJust, isNothing,<br>
fromJust, fromMaybe, listToMaybe, maybeToList,<br>
catMaybes, mapMaybe,<br>
<br>
-- ...and what the Prelude exports<br>
Maybe(Nothing, Just),<br>
maybe<br>
) where<br>
<br>
isJust :: Maybe a -> Bool<br>
isJust (Just a) = True<br>
isJust Nothing = False<br>
<br>
isNothing :: Maybe a -> Bool<br>
isNothing = not . isJust<br>
<br>
fromJust :: Maybe a -> a<br>
fromJust (Just a) = a<br>
fromJust Nothing = error "Maybe.fromJust: Nothing"<br>
<br>
fromMaybe :: a -> Maybe a -> a<br>
fromMaybe d Nothing = d<br>
fromMaybe d (Just a) = a<br>
<br>
maybeToList :: Maybe a -> [a]<br>
maybeToList Nothing = []<br>
maybeToList (Just a) = [a]<br>
<br>
listToMaybe :: [a] -> Maybe a<br>
listToMaybe [] = Nothing<br>
listToMaybe (a:_) = Just a<br>
<br>
catMaybes :: [Maybe a] -> [a]<br>
catMaybes ms = [ m | Just m <- ms ]<br>
<br>
mapMaybe :: (a -> Maybe b) -> [a] -> [b]<br>
mapMaybe f = catMaybes . map f<br>
<p>
<hr><i>The Haskell 98 Library Report</i><br><a href="index.html">top</a> | <a href="list.html">back</a> | <a href="char.html">next</a> | <a href="libindex.html">contents</a> <br><font size=2>1 February, 1999</font>
</tt>
|