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 to do it...

  1. Create a new project working-with-monad using the simple Stack template:
        stack new working-with-monad simple
  1. Open src/Main.hs and edit it.
  1. 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.
  1. Import other required headers now. The header for monad is Control.Monad:
        import Data.Functor
        import Control.Applicative
        import Control.Monad
  1. 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 ...

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