Difference between revisions of "Solution1.html"

From HaskellWiki
Jump to: navigation, search
(New page: <haskell> maternalGrandfather :: Sheep -> Maybe Sheep maternalGrandfather s = return s >>= \ms -> mother ms >>= \m -> father m fathersMaternalGrandmother :: Sheep -> Mayb...)
 
m (Better organisation of content needed...)
Line 43: Line 43:
 
father gf
 
father gf
 
</haskell>
 
</haskell>
  +
  +
<!-- atravers: Have one single page of solutions, instead of a page for each solution... -->
  +
[[Category:Pages under construction]]

Revision as of 03:52, 9 April 2021

maternalGrandfather :: Sheep -> Maybe Sheep
maternalGrandfather s =
   return s    >>= \ms  ->
   mother ms   >>= \m   ->
   father m

fathersMaternalGrandmother :: Sheep -> Maybe Sheep
fathersMaternalGrandMother s =
   return s    >>= \ms  ->
   father ms   >>= \f   ->
   mother s    >>= \gm  ->
   mother gm

mothersPaternalGrandfather :: Sheep -> Maybe Sheep
mothersPaternalGrandfather s = 
   return s    >>= \ms  ->
   mother ms   >>= \m   ->
   father m    >>= \gf  ->
   father gf

Note: the returns are not not necessary; they are only used for the sake of the exercise.

An alternative solution without use of return:

maternalGrandfather :: Sheep -> Maybe Sheep
maternalGrandfather s =
   mother s    >>= \m   ->
   father m

fathersMaternalGrandmother :: Sheep -> Maybe Sheep
fathersMaternalGrandMother s =
   father s    >>= \f   ->
   mother s    >>= \gm  ->
   mother gm

mothersPaternalGrandfather :: Sheep -> Maybe Sheep
mothersPaternalGrandfather s = 
   mother s    >>= \m   ->
   father m    >>= \gf  ->
   father gf