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-applicativewith thesimpleStack template:
stack new working-with-applicative simple
- Open
src/Main.hsand add the following imports after the initial module definition. TheApplicativetype class is defined in the moduleControl.Applicative:
import Data.Functor
import Control.Applicative- We will use two operators,
Functorapplication<$>(synonym forfmap) andApplicativeapplication<*>. TheApplicativefunction<*>is...