Guest User

Untitled

a guest
Jan 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import Control.Monad
  2. import Data.Maybe
  3.  
  4. maybeToMonad :: (MonadPlus m) => Maybe a -> m a
  5. maybeToMonad Nothing = mzero
  6. maybeToMonad (Just x) = return x
  7.  
  8. type Sheep = Int
  9.  
  10. father :: Sheep -> Maybe Sheep
  11. father 1 = Just 2
  12. father 2 = Just 4
  13. father 3 = Just 11
  14. father 5 = Just 12
  15. father _ = Nothing
  16.  
  17. mother :: Sheep -> Maybe Sheep
  18. mother 1 = Just 3
  19. mother 2 = Just 5
  20. mother 3 = Just 10
  21. mother _ = Nothing
  22.  
  23. parent2 :: Sheep -> Maybe Sheep
  24. parent2 s = (mother s) `mplus` (father s)
  25.  
  26. grandparent2 :: Sheep -> Maybe Sheep
  27. grandparent2 s = (parent2 s) >>= parent2
  28.  
  29. parent3 :: Sheep -> [Sheep]
  30. parent3 s = (maybeToList $ mother s) `mplus` (maybeToList $ father s)
  31.  
  32. grandparent3 :: Sheep -> [Sheep]
  33. grandparent3 s = (parent3 s) >>= parent3
  34.  
  35. parent4 :: (MonadPlus m) => Sheep -> m Sheep
  36. parent4 s = (maybeToMonad $ mother s) `mplus` (maybeToMonad $ father s)
  37.  
  38. grandparent4 :: (MonadPlus m) => Sheep -> m Sheep
  39. grandparent4 s = (parent4 s) >>= parent4
Add Comment
Please, Sign In to add comment