Solution to Exercise 3

The definition of parent in the List monad is very similar to the definition in the Maybe monad. The only difference is the need to convert the values returned by mother and father from the Maybe monad to the List monad using the Maybe.maybeToList function.

Code available in exercise3.hs
parent :: Sheep -> [Sheep]
parent s = (maybeToList (mother s)) `mplus` (maybeToList (father s))

Unlike our definition of grandparent in the Maybe monad, we can use the simpler form in the List monad since the List monad does implement a backtracking strategy:

Code available in exercise3.hs
grandparent :: Sheep -> [Sheep]
grandparent s = do p <- parent s
                   parent p

Return to exercises.