28 Jul 2012 17:00

## MonadPlus m => Maybe a -> m a

```I'm solving this exercise:

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
```

28 Jul 2012 18:17

### Re: MonadPlus m => Maybe a -> m a

On Sat, Jul 28, 2012 at 8:00 AM, Thiago Negri wrote:
I'm solving this exercise:

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?

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

I doubt there is a "standard" named function for this, since conversions between monads are typically not "unique" and there are lots and lots of pairs of monads you can convert between.

You can define g in terms of Data.Maybe.maybe as:

g = maybe mzero return
```_______________________________________________