<div dir="ltr">In the book "Haskell programming from first principles" it is said that:<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">If you are using GHC 7.10 or newer, you’ll see an Applicative constraint in the definition of Monad, as it should be:</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">    class Applicative m => Monad m where<br>        (>>=) :: m a -> (a -> m b) -> m b<br>        (>>) :: m a -> m b -> m b<br>        return :: a -> m a</blockquote><br>I have created the following applicative functor.<br><br>data WhoCares a = ItDoesnt | Matter a | WhatThisIsCalled deriving (Eq, Show)<br><br>instance Functor WhoCares where<br>    fmap _ ItDoesnt = ItDoesnt<br>    fmap _ WhatThisIsCalled = WhatThisIsCalled<br>    fmap f (Matter a) = Matter (f a)<br><br>instance Applicative WhoCares where<br>    pure = Matter<br>    Matter f <*> Matter a = Matter (f a)<br><br>main = do<br><br>    -- fmap id == id<br>    let funcx = fmap id "Hi Julie"<br>    let funcy = id "Hi Julie"<br>    print(funcx)<br>    print(funcy)<br>    print(funcx == funcy)<br><br>    -- fmap (f . g) == fmap f . fmap g<br>    let funcx' = fmap ((+1) . (*2)) [1..5]<br>    let funcy' = fmap (+1) . fmap (*2) $ [1..5]<br>    print(funcx')<br>    print(funcy')<br>    print(funcx' == funcy')<br><br>    -- pure id <*> v = v<br>    print(pure id <*> (Matter 10))<br><br>    -- pure (.) <*> u <*> v <*> w = u <*> (v <*> w)<br>    let appx = pure (.) <*> (Matter (+1)) <*> (Matter (*2)) <*> (Matter 10)<br>    let appy = (Matter (+1)) <*> ((Matter (*2)) <*> (Matter 10))<br>    print(appx)<br>    print(appy)<br>    print(appx == appy)<br><br>    -- pure f <*> pure x = pure (f x)<br>    let appx' = pure (+1) <*> pure 1 :: WhoCares Int<br>    let appy' = pure ((+1) 1) :: WhoCares Int<br>    print(appx')<br>    print(appy')<br>    print(appx' == appy')<br><br>    -- u <*> pure y = pure ($ y) <*> u<br>    let appx'' = Matter (+2) <*> pure 2<br>    let appy'' = pure ($ 2) <*> Matter (+ 2)<br>    print(appx'')<br>    print(appy'')<br>    print(appx'' == appy'')<br><br>Due to lack of examples, I am not understanding how to implement >>= and >>. The code I came up with so far is:<br><br>instance Monad (WhoCares a) where<br>    (>>=) :: Matter a -> (a -> Matter b) -> Matter b<br>    (>>) :: Matter a -> Matter b -> Matter b<br>    return :: a -> Matter a<br>    return = pure<br><br>So, that I can do stuff like:<br><br>half x = if even x<br>            then Matter (x `div` 2)<br>            else ItDoesnt<br><br>incVal :: (Ord a, Num a) => a -> WhoCares a<br>incVal x<br>    | x + 1 <= 10 = return (x + 1)<br>    | otherwise = ItDoesnt<br><br>decVal :: (Ord a, Num a) => a -> WhoCares a<br>decVal x<br>    | x - 1 >= 0 = return (x - 1)<br>    | otherwise = ItDoesnt<br><br>main = do<br>    print (Matter 7 >>= incVal >>= incVal >>= incVal)<br>    print (Matter 7 >>= incVal >>= incVal >>= incVal >>= incVal)<br>    print (Matter 7 >>= incVal >>= incVal >>= incVal >>= incVal >>= decVal >>= decVal)<br>    print (Matter 2 >>= decVal >>= decVal >>= decVal)<br>    print(Matter 20 >>= half >>= half)<br><br>With Output:<br><br>10<br>ItDoesnt<br>ItDoesnt<br>ItDoesnt<br>5<br><br>Please help.<br><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><b style="font-weight:normal"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:10pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Thanks and Best Regards,</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:10pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Ahmad Ismail</span></p></b></div></div></div></div>