Difference between revisions of "Solution2.html"

From HaskellWiki
Jump to navigation Jump to search
(New page: <haskell> parent :: Sheep -> Maybe Sheep parent s = father s `mplus` mother s grandparent :: Sheep -> Maybe Sheep grandparent s = paternalGrandfather s `mplus` paternalGr...)
 
m (Contents transferred to single solutions page)
 
(4 intermediate revisions by 2 users not shown)
Line 9: Line 9:
 
maternalGrandmother s
 
maternalGrandmother s
 
</haskell>
 
</haskell>
  +
  +
  +
This next solution will not work. If the sheep has a father and only a maternal grandparent, this function will return Nothing:
  +
<haskell>
  +
grandparent :: Sheep -> Maybe Sheep
  +
grandparent s = parent s >>= parent
  +
</haskell>
  +
  +
[[Category:Pages to be removed]]

Latest revision as of 08:43, 9 April 2021

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