[Haskell-beginners] How to create a monad in GHC 7.10 or newer

Francesco Ariis fa-ml at ariis.it
Sun Nov 13 15:21:49 UTC 2022


Il 13 novembre 2022 alle 20:56 Ahmad Ismail ha scritto:
> How can I fix it so that `ItDoesnt <*> WhatThisIsCalled` works?
> 
> I have came up with a solution without WhatThisIsCalled
> 
> data WhoCares a = ItDoesnt | Matter a deriving (Eq, Show)
> 
> instance Functor WhoCares where
>     fmap _ ItDoesnt = ItDoesnt
>     fmap f (Matter a) = Matter (f a)
> 
> instance Applicative WhoCares where
>     pure = Matter
>     Matter f <*> Matter a = Matter (f a)
>     ItDoesnt <*> _ = ItDoesnt
>     _ <*> ItDoesnt = ItDoesnt
> 
> instance Monad WhoCares where
>     return x = Matter x
>     (Matter x) >>= k = k x
>     ItDoesnt >>= _ = ItDoesnt

This makes much more sense. Now <*> is total and >>= is meaningful,
well done!


More information about the Beginners mailing list