Solution3.html

From HaskellWiki
Revision as of 14:32, 21 September 2012 by Gphilip (talk | contribs) (Another solution.)
Jump to navigation Jump to search
parent :: Sheep -> [Sheep]
parent s = (maybeToList (mother s)) ++ (maybeToList (father s))

grandparent :: Sheep -> [Sheep]
grandparent s = (maybeToList (paternalGrandfather s)) ++
                (maybeToList (paternalGrandmother s)) ++
                (maybeToList (maternalGrandfather s)) ++
                (maybeToList (maternalGrandmother s))

Alternate solution:

parent :: Sheep -> [Sheep]
parent s = (maybeToList $ mother s) `mplus` (maybeToList $ father s)

grandparent :: Sheep -> [Sheep]
grandparent s = parent s >>= parent