Maybe
From HaskellWiki
BrettGiles (Talk | contribs) (Add a short section on some of the functions supplied with Data.Maybe) |
(removed redundant nowiki tag) |
||
(8 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{Standard type|Maybe|module=Data.Maybe|module-doc=Data-Maybe|package=base}} | {{Standard type|Maybe|module=Data.Maybe|module-doc=Data-Maybe|package=base}} | ||
The '''Maybe''' type is defined as follows: | The '''Maybe''' type is defined as follows: | ||
− | + | <haskell> | |
data Maybe a = Just a | Nothing | data Maybe a = Just a | Nothing | ||
deriving (Eq, Ord) | deriving (Eq, Ord) | ||
− | + | </haskell> | |
It allows the programmer to specify something may not be there. | It allows the programmer to specify something may not be there. | ||
− | ==Type | + | ==Type equation== |
− | '''Maybe''' satisfies the [[type]] equation <math>F X = 1 + | + | '''Maybe''' satisfies the [[type]] equation <math>F X = 1 + X</math>, where the functor <math>F</math> takes a set to a point plus that set. |
==Comparison to imperative languages== | ==Comparison to imperative languages== | ||
− | + | Imperative languages may support this by rewriting as a ''union'' or allow one to use / return ''NULL'' (defined in some manner) to specify a value might not be there. | |
==Classes== | ==Classes== | ||
Line 23: | Line 23: | ||
always be '''Nothing'''. | always be '''Nothing'''. | ||
− | === | + | ===Maybe as a Monad=== |
Using the [[Monad]] class definition can lead to much more compact code. For example: | Using the [[Monad]] class definition can lead to much more compact code. For example: | ||
− | + | <haskell> | |
f::Int -> Maybe Int | f::Int -> Maybe Int | ||
f 0 = Nothing | f 0 = Nothing | ||
Line 42: | Line 42: | ||
h' x = do n <- f x | h' x = do n <- f x | ||
g n | g n | ||
− | + | </haskell> | |
The functions <code>h</code> and <code>h'</code> will give the same results. (<math>h 0 = h' 0 = h 100 = h' 100 = Nothing;\ h x = h' x = Just\, x</math>). In this case the savings in code size is quite modest, stringing together multiple functions like <code>f</code> and <code>g</code> will be more noticeable. | The functions <code>h</code> and <code>h'</code> will give the same results. (<math>h 0 = h' 0 = h 100 = h' 100 = Nothing;\ h x = h' x = Just\, x</math>). In this case the savings in code size is quite modest, stringing together multiple functions like <code>f</code> and <code>g</code> will be more noticeable. | ||
− | ==Library | + | ==Library functions== |
When the module is imported, it supplies a variety of useful functions including: | When the module is imported, it supplies a variety of useful functions including: | ||
− | ;< | + | ;<hask>maybe :: b->(a->b) -> Maybe a -> b</hask> :Applies the second argument to the third, when it is ''Just x'', otherwise returns the first argument. |
− | ;< | + | ;<hask>isJust</hask>, <hask>isNothing</hask> :Test the argument, returing a Bool based on the constructor. |
− | ;< | + | ;<hask>listToMaybe</hask>, <hask>maybeToList</hask> :Convert to/from a one element or empty list. |
− | ;< | + | ;<hask>mapMaybe</hask> : A different way to filter a list. |
− | See the documentation for [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html Data.Maybe] for more | + | See the documentation for [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html Data.Maybe] for more explanation and other functions. |
+ | [[Category:Monad]] |
Latest revision as of 13:53, 30 April 2012
import Data.Maybe |
The Maybe type is defined as follows:
data Maybe a = Just a | Nothing deriving (Eq, Ord)
It allows the programmer to specify something may not be there.
Contents |
[edit] 1 Type equation
Maybe satisfies the type equation FX = 1 + X, where the functor F takes a set to a point plus that set.
[edit] 2 Comparison to imperative languages
Imperative languages may support this by rewriting as a union or allow one to use / return NULL (defined in some manner) to specify a value might not be there.
[edit] 3 Classes
As one can see from the type definition, Maybe will be an instance of Eq and Ord when the base type is. As well, instances of Functor and Monad are defined for Maybe.
For Functor, the fmap
function moves inside the Just constructor and is identity on the Nothing constructor.
For Monad, the bind operation passes through Just, while Nothing will force the result to always be Nothing.
[edit] 3.1 Maybe as a Monad
Using the Monad class definition can lead to much more compact code. For example:
f::Int -> Maybe Int f 0 = Nothing f x = Just x g :: Int -> Maybe Int g 100 = Nothing g x = Just x h ::Int -> Maybe Int h x = case f x of Just n -> g n Nothing -> Nothing h' :: Int -> Maybe Int h' x = do n <- f x g n
The functions h
and h'
will give the same results. (). In this case the savings in code size is quite modest, stringing together multiple functions like f
and g
will be more noticeable.
[edit] 4 Library functions
When the module is imported, it supplies a variety of useful functions including:
- maybe :: b->(a->b) -> Maybe a -> b
- Applies the second argument to the third, when it is Just x, otherwise returns the first argument.
- ,isJustisNothing
- Test the argument, returing a Bool based on the constructor.
- ,listToMaybemaybeToList
- Convert to/from a one element or empty list.
- mapMaybe
- A different way to filter a list.
See the documentation for Data.Maybe for more explanation and other functions.