Personal tools

Solution2.html

From HaskellWiki

(Difference between revisions)
Jump to: navigation, 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
 
(3 intermediate revisions by 2 users not shown)
Line 8: Line 8:
 
                 maternalGrandfather s `mplus`  
 
                 maternalGrandfather s `mplus`  
 
                 maternalGrandmother s
 
                 maternalGrandmother s
 +
</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>
 
</haskell>

Latest revision as of 17:49, 10 June 2013

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