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. Start editing src/Main.hsAdd import Data.Monoid at the top. This module contains the definition of the Monoid type class. 
  2. Define a data type Option. The data contains a Boolean field and a list of String:
        data Option = Option { boolOption :: Bool, selections ::         [String] }            deriving Show
  1. Define the instance of Monoid. The Monoid class needs to define at minimum two functions, mempty and mappend
        instance Monoid Option where        mempty = Option False []       (Option b1 s1) `mappend` (Option b2 s2) = Option (b1 || b2) (s1        ++ s2)
  1. Use the Option data type and its Monoid instance in the main function:
        main :: IO ()        main = do        putStrLn "Define default options"        let defaultOptions = mempty :: Option putStrLn (show defaultOptions) ...

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