## Want to keep learning?

This content is taken from the The University of Glasgow's online course, Functional Programming in Haskell: Supercharge Your Coding. Join the course to learn more.
6.11

## The University of Glasgow Here be dragons

• We’ve already seen the Maybe type. Let’s look at the Maybe monad, which makes using the Maybe type a lot easier.

### The Maybe type constructor

You already know the definition of the Maybe type:

    data Maybe a = Just a | Nothing


### Example use of Maybe: Safe head and tail

The head and tail functions from the Prelude are not safe in the sense that they fail when called on an empty list. We can define safe versions using Maybe:

    myHead :: [a] -> Maybe a

myTail :: [a] -> Maybe [a]
myTail [] = Nothing
myTail (x:xs) = Just xs


Now we can make Maybe an instance of the Monad type class, simply by providing the appropriate definitions for return, bind, then and fail:

    import Control.Monad

return           =   Just
Nothing  >>= f = Nothing
(Just x) >>= f = f x
fail _           =   Nothing


There are a few additional functions defined in the MonadPlus type class:

    instance MonadPlus Maybe where
mzero               = Nothing
Nothing mplus x = x
x mplus _         = x


That’s it, we now have a Maybe monad!

Note: for users of ghc 7.10 and higher, we need to do a little bit more work.

## Explicit Maybe versus the Maybe Monad

Let’s see what this monad gives us:

### A computation using explicit Maybe

    foo :: [a] -> Maybe a
foo xs =
case myTail xs of
Nothing -> Nothing
Just a -> case myTail a of
Nothing -> Nothing


To combine computations that use the Maybe type, we need explicit case expressions to pattern match against the type.

### A computation using the Maybe monad

Let’s write this computation using the Maybe monad, first using the >>= operator:

    bar :: [a] -> Maybe a
bar xs =
myTail xs >>=
(\a -> myTail a >>=


Now let’s change the line breaks and indentation a bit to make it look nicer:

    bar2 :: [a] -> Maybe a
bar2 xs =
myTail xs >>= (\a ->
myTail a >>=  (\b ->


Thanks to the associativity law, we can also remove unnecessary parentheses:

    bar3 :: [a] -> Maybe a
bar3 xs =
myTail xs >>= \a ->
myTail a >>=  \b ->


This is already a lot cleaner, but finally we can use the do-notation:

    bar3 :: [a] -> Maybe a
bar3 xs = do
a <- myTail xs
b <- myTail a


### Example: Reduction of bar [5,6]

        bar [5,6]

-- > substitute [5,6] for xs in definition of bar

myTail [5,6] >>=
(\a -> myTail a >>=

-- > def. myTail

Just   >>=
(\a -> myTail a >>=

-- >  def.2 of (>>=)

(\a -> myTail a >>=

-- > beta reduction, substitute  for a

myTail  >>= (\b -> myHead b)

-- > reduce myTail

Just [] >>=  (\b -> myHead b)

-- >  def.2 of (>>=)

-- > beta reduction, substitute [] for b

Nothing


### Example: Reduction of bar [5,6,7]

        bar [5,6,7]

-- > substitute [5,6,7] for xs in definition of bar

myTail [5,6,7] >>=
(\a -> myTail a >>=

-- > def. myTail

Just [6,7]  >>=
(\a -> myTail a >>=

-- >  def.2 of (>>=)

(\a -> myTail a >>=
[6,7]
-- > beta reduction, substitute [6,7] for a

myTail [6,7] >>= (\b -> myHead b)

-- > reduce myTail

Just  >>=  (\b -> myHead b)

-- >  def.2 of (>>=)

-- > beta reduction, substitute  for b