Skip to content

Instantly share code, notes, and snippets.

@kbridge
Created September 25, 2021 17:11
Show Gist options
  • Save kbridge/84f79b2c693ad6cca47a22ccfe001286 to your computer and use it in GitHub Desktop.
Save kbridge/84f79b2c693ad6cca47a22ccfe001286 to your computer and use it in GitHub Desktop.
Forever/MaybeT Demo
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