Skip to content

Instantly share code, notes, and snippets.

@dmatveev
Created February 27, 2012 06:57
Show Gist options
  • Save dmatveev/1922080 to your computer and use it in GitHub Desktop.
Save dmatveev/1922080 to your computer and use it in GitHub Desktop.
A poor man's monad
(>>?) :: (Ord a, Num a) => Maybe a -> (a -> Maybe b) -> Maybe b
Nothing >>? _ = Nothing
Just v >>? f = if v > 0 then f v else Nothing
decr :: Int -> Maybe Int
decr a = Just (a - 1)
ex1 = (Just 1) >>? decr >>? decr >>? decr
ex2 = (Just 10) >>? decr >>? decr >>? decr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment