Created
July 10, 2015 17:06
-
-
Save gfixler/b5002dfacbd972aea6e8 to your computer and use it in GitHub Desktop.
This file contains 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
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