[Haskell-cafe] MonadPlus m => Maybe a -> m a

Thiago Negri evohunz at gmail.com
Sat Jul 28 17:00:24 CEST 2012


I'm solving this exercise:
http://www.haskell.org/haskellwiki/All_About_Monads#Exercise_4:_Using_the_Monad_class_constraint

I'm missing a function to transform a Maybe a into a MonadPlus m => m a.
I did search on Hoogle with no luck.

There is no standard definition for the "g" function I'm defining?

My take on the exercise:

data Sheep = Sheep {
        mother :: Maybe Sheep,
        father :: Maybe Sheep,
        name :: String }

instance Show Sheep where -- for testing
        show = name

g :: (MonadPlus m) => Maybe a -> m a
g Nothing = mzero
g (Just a) = return a

mother' :: (MonadPlus m) => Sheep -> m Sheep
mother' = g . mother

father' :: (MonadPlus m) => Sheep -> m Sheep
father' = g . father

parent'' :: (MonadPlus m) => Sheep -> m Sheep
parent'' s = mother' s `mplus` father' s

grandparent'' :: (MonadPlus m) => Sheep -> m Sheep
grandparent'' s = parent'' s >>= parent''



More information about the Haskell-Cafe mailing list