Skip to content

Instantly share code, notes, and snippets.

@gfixler

gfixler/maybeIO.hs

Created Jul 10, 2015
Embed
What would you like to do?
newtype MaybeIO a = MaybeIO (IO (Maybe a))
runMaybeIO :: MaybeIO a -> IO (Maybe a)
runMaybeIO (MaybeIO x) = x
hole = undefined
data Hole = Hole
instance Monad MaybeIO where
return x = MaybeIO (return (Just x))
(MaybeIO x) >>= k = MaybeIO $ do
y <- x
case y of Just z -> let (MaybeIO z') = k z in z'
Nothing -> return Nothing
readMaybe :: (Read a) => String -> Maybe a
readMaybe x = case reads x of
[(name,"")] -> Just name
_ -> Nothing
maybeIONumber :: MaybeIO Int
maybeIONumber = MaybeIO $ do
x <- getLine
return $ readMaybe x
maybeIONumber' :: MaybeIO Int
maybeIONumber' = liftFromIOToMaybeIO getLine
>>= liftFromMaybeToMaybeIO . readMaybe
maybeIONumberWithPlus :: Int -> MaybeIO Int
maybeIONumberWithPlus x = liftFromIOToMaybeIO getLine
>>= liftFromMaybeToMaybeIO . readMaybe
liftFromIOToMaybeIO :: IO a -> MaybeIO a
liftFromIOToMaybeIO x = MaybeIO $ do
y <- x
return (return y)
liftFromMaybeToMaybeIO :: Maybe a -> MaybeIO a
liftFromMaybeToMaybeIO = MaybeIO . return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment