Solution2.html

From HaskellWiki
Revision as of 17:49, 10 June 2013 by Prajit (talk | contribs) (Explained an incorrect solution,)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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 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