Solution2.html

From HaskellWiki
Revision as of 08:43, 9 April 2021 by Atravers (talk | contribs) (Contents transferred to single solutions page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
parent :: Sheep -> Maybe Sheep
parent s = father s `mplus` mother s

grandparent :: Sheep -> Maybe Sheep
grandparent s = paternalGrandfather s `mplus` 
                paternalGrandmother s `mplus` 
                maternalGrandfather s `mplus` 
                maternalGrandmother s


This next solution will not work. If the sheep has a father and only a maternal grandparent, this function will return Nothing:

grandparent :: Sheep -> Maybe Sheep
grandparent s = parent s >>= parent