The following function f tries to read Int twice, using the IO (Maybe Int) function twice, but performing "short circuits" after successfully reading one Int :
readInt :: IO (Maybe Int) f :: IO (Maybe Int) f = do n1 <- readInt case n1 of Just n' -> return (Just n') Nothing -> do n2 <- readInt case n2 of Just n' -> return (Just n') Nothing -> return Nothing
Is there a good way to reorganize this code? It would be very hairy if I extended it to three attempts ...
(My thought process: seeing this "staircasing", tells me that maybe I should use a Monad Maybe instance, but since it is already in the IO monad, I will need to use MaybeT (?) MaybeT . However, I only need one of readInt to succeed, so the behavior of the Maybe monad error on the first Nothing error would be wrong here ...)
haskell monads io-monad maybe
beta
source share