Created
September 25, 2021 17:11
-
-
Save kbridge/84f79b2c693ad6cca47a22ccfe001286 to your computer and use it in GitHub Desktop.
Forever/MaybeT Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maybeBind :: IO (Maybe a) -> (a -> IO (Maybe b)) -> IO (Maybe b) | |
maybeBind x f = | |
x >>= \a -> | |
case a of | |
Just y -> f y | |
Nothing -> return Nothing | |
getLineMaybe :: IO (Maybe String) | |
getLineMaybe = getLine >>= \line -> return $ if null line then Nothing else Just line | |
program1 :: IO (Maybe ()) | |
program1 = getLineMaybe `maybeBind` \line -> fmap Just (putStrLn $ "result: " ++ (map toUpper line)) | |
foreverMaybe :: IO (Maybe a) -> IO (Maybe b) | |
foreverMaybe m = m `maybeBind` \_ -> foreverMaybe m | |
fmap (const ()) (foreverMaybe program1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment