Difference between revisions of "Solution4.html"

From HaskellWiki
Jump to navigation Jump to search
(Another definition of grandparent.)
m (Contents transferred to single solutions page)
 
(One intermediate revision by one other user not shown)
Line 16: Line 16:
 
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:
 
  +
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):
 
<haskell>
 
<haskell>
 
grandparent :: (MonadPlus m) => Sheep -> m Sheep
 
grandparent :: (MonadPlus m) => Sheep -> m Sheep
 
grandparent s = parent s >>= parent
 
grandparent s = parent s >>= parent
 
</haskell>
 
</haskell>
  +
  +
[[Category:Pages to be removed]]

Latest revision as of 08:45, 9 April 2021

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

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


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