- Create a new project working-with-monad using the simple Stack template:
stack new working-with-monad simple
- Open src/Main.hs and edit it.
- After the initial module definition, add the following imports:
import Prelude hiding(Maybe(..))
Note that we are importing otherwise implicitly imported module Prelude explicitly by hiding Maybe. This is because we are defining our own Maybe data type.
- Import other required headers now. The header for monad is Control.Monad:
import Data.Functor import Control.Applicative import Control.Monad
- Now, define the Maybe data type. Also, define the instance for Functor and Applicative for it:
data Maybe a = Nothing | Just a deriving Show instance Functor Maybe where fmap f (Just ...