[Haskell-beginners] How to create a monad in GHC 7.10 or newer
Francesco Ariis
fa-ml at ariis.it
Sun Nov 13 11:07:38 UTC 2022
Hello Ahmad,
Il 13 novembre 2022 alle 16:33 Ahmad Ismail ha scritto:
> Due to lack of examples, I am not understanding how to implement >>= and
> >>.
All you need to implement is (>>=)!
> The code I came up with so far is:
>
> instance Monad (WhoCares a) where
> (>>=) :: Matter a -> (a -> Matter b) -> Matter b
> (>>) :: Matter a -> Matter b -> Matter b
> return :: a -> Matter a
> return = pure
The signature for (>>=) is wrong, `Matter` is a *data* constructor, you
need a *type* one instead, so:
(>>=) :: WhoCares a -> (a -> WhoCares b) -> WhoCares b
But let us go back to typeclasses. Your `Applicative` instance
> instance Applicative WhoCares where
> pure = Matter
> Matter f <*> Matter a = Matter (f a)
is broken:
λ> ItDoesnt <*> WhatThisIsCalled
*** Exception: /tmp/prova.hs:11:5-40: Non-exhaustive patterns in function <*>
So we need first to fix that. What behaviour would you expect, what are
you trying to model with `WhoCares`?
—F
More information about the Beginners
mailing list