Skip to content

Instantly share code, notes, and snippets.

@eiel
Created Jul 3, 2016
Embed
What would you like to do?
MaybeTについて学んだ
module Main where
import System.Environment
import Data.Maybe
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Class
-- sample >>= fromMaybe (putStrLn "not found")
sample1= do
maybeHoge <- lookupEnv "Hoge"
return $ do
hoge <- maybeHoge
return $ putStrLn hoge
sample1_5 = do
maybeHoge <- lookupEnv "Hoge"
case maybeHoge of
Just hoge -> putStrLn hoge >> return (Just ())
Nothing -> return Nothing
-- sample2
sample2 = do
maybeHoge <- lookupEnv "Hoge" :: IO (Maybe String)
let hoge = fromMaybe "not found" maybeHoge
putStrLn hoge
-- sample3
sample3 = do
hoge <- MaybeT $ lookupEnv "Hoge"
lift $ putStrLn hoge
sample4 = do
maybe <- runMaybeT sample3
case maybe of
Nothing -> putStrLn "not found"
_ -> return ()
sample5 = do
maybe <- sample1_5
case maybe of
Nothing -> putStrLn "not found"
_ -> return ()
main = runMaybeT sample3
IoMaybeA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment