Skip to content

Instantly share code, notes, and snippets.

@skyscribe
Created March 23, 2012 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyscribe/2168956 to your computer and use it in GitHub Desktop.
Save skyscribe/2168956 to your computer and use it in GitHub Desktop.
MonadSheep
data Sheep = SheepCreator String (Sheep, Sheep) | NONE
deriving Show
father:: Sheep -> Maybe Sheep
father (SheepCreator name (NONE, _)) = Nothing
father (SheepCreator name (f, _)) = Just f
mother:: Sheep -> Maybe Sheep
mother (SheepCreator name (_, NONE)) = Nothing
mother (SheepCreator name (_, m)) = Just m
maternalGrandfather :: Sheep -> Maybe Sheep
maternalGrandfather s = (return s) >>= mother >>= father
maternalGrandfatherDo :: Sheep -> Maybe Sheep
maternalGrandfatherDo s = do m <- mother s
mf <- father m
return mf
fathersMaternalGrandmother :: Sheep -> Maybe Sheep
fathersMaternalGrandmother s = (return s) >>= father >>= mother >>= mother
fathersMaternalGrandmotherDo :: Sheep -> Maybe Sheep
fathersMaternalGrandmotherDo s = do f <- father s
mf <- mother f
mmf <- mother mf
return mmf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment