Solution4.html
From HaskellWiki
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]
This next alternative grandparent function only works in the case of the List monad (see exercise 5.2 for why the Maybe monad does not work):
grandparent :: (MonadPlus m) => Sheep -> m Sheep grandparent s = parent s >>= parent
