# Solution4.html

### From HaskellWiki

(Difference between revisions)

(New page: <haskell> parent :: MonadPlus m => Sheep -> m Sheep parent s = (toMonad (father s)) `mplus` (toMonad (mother s)) grandparent :: MonadPlus m => Sheep -> m Sheep grandparent s = (toMonad (p...) |
(Another definition of grandparent.) |
||

Line 15: | Line 15: | ||

If the compiler cannot guess which MonadPlus to use you will need to specify it when the function is called. So, <hask>parent someSheep :: Maybe Sheep</hask> will use the Maybe monad and either <hask>parent someSheep :: [] Sheep</hask> or <hask>parent someSheep :: [Sheep]</hask> will use the list monad. |
If the compiler cannot guess which MonadPlus to use you will need to specify it when the function is called. So, <hask>parent someSheep :: Maybe Sheep</hask> will use the Maybe monad and either <hask>parent someSheep :: [] Sheep</hask> or <hask>parent someSheep :: [Sheep]</hask> will use the list monad. |
||

+ | |||

+ | Alternative grandparent: |
||

+ | <haskell> |
||

+ | grandparent :: (MonadPlus m) => Sheep -> m Sheep |
||

+ | grandparent s = parent s >>= parent |
||

+ | </haskell> |

## Revision as of 18:01, 21 September 2012

parent :: MonadPlus m => Sheep -> m Sheep parent s = (toMonad (father s)) `mplus` (toMonad (mother s)) grandparent :: MonadPlus m => Sheep -> m Sheep grandparent s = (toMonad (parentalGrandfather s)) `mplus` (toMonad (parentalGrandmother s)) `mplus` (toMonad (maternalGrandfather s)) `mplus` (toMonad (maternalGrandmother s)) toMonad :: MonadPlus m => Maybe a -> m a toMonad Nothing = mzero toMonad (Just s) = return s

parent someSheep :: Maybe Sheep

parent someSheep :: [] Sheep

parent someSheep :: [Sheep]

Alternative grandparent:

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