Working with Applicatives
An Applicative
is a type class that is somewhere between a Functor
and a Monad
. An Applicative
takes a Functor
one step further. A Functor
talks about application of a function a -> b
to a data type f a
, whereas an Applicative
talks about application of a data type of a function f (a -> b)
to a data type f a
.
In this recipe, we will work with Maybe
and Either
data types, and see how we can work with Applicative
instances in the context of these data types.
How to do it...
- Create a new project
working-with-applicative
with thesimple
Stack template:
stack new working-with-applicative simple
- Open
src/Main.hs
and add the following imports after the initial module definition. TheApplicative
type class is defined in the moduleControl.Applicative
:
import Data.Functor import Control.Applicative
- We will use two operators,
Functor
application<$>
(synonym forfmap
) andApplicative
application<*>
. TheApplicative
function<*>
is...