O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

One can look at Monad as a logical extension of Applicative, but with stronger implications. The monad type class is defined as follows:

    class Applicative m => Monad (m :: * -> *) where     (>>=) :: m a -> (a -> m b) -> m b     (>>) :: m a -> m b -> m b     return :: a -> m a     fail :: String -> m a     {-# MINIMAL (>>=) #-}

To define the monad instance, one needs to define the binding function (>>=). In fact, the return function is equivalent to the Applicative pure function. 

The binding function is interesting, with the following signature:

    (>>=) :: m a -> (a -> m b) -> m b

To interpret the preceding function in the context of Maybe, consider the following illustration:

The return function that is equivalent to pure takes in a value and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required