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.
Maybe satisfies the type equation , where the functor takes a set to a point plus that set.
Comparison to imperative languages
Most imperative languages will ignore this, or allow one to use NULL (defined in some manner) to specify a value might not be there.
For Monad, the bind operation passes through Just, while Nothing will force the result to always be Nothing.
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
h' will give the same results. (). In this case the savings in code size is quite modest, stringing together multiple functions like
g will be more noticeable.