Last active
December 11, 2015 09:58
-
-
Save apstndb/4583145 to your computer and use it in GitHub Desktop.
第7回 スタートHaskell2演習問題「Readerモナドを使ってみよう」解答 http://wiki.haskell.jp/Workshop/StartHaskell2/exercise14
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
import Data.List | |
import System.Environment | |
import Data.Maybe | |
import Control.Monad.Reader | |
type Env = [(String, String)] | |
environment :: Env | |
environment = | |
[ ("HOME", "/home/haskell") | |
, ("PATH", "/bin:/usr/bin") | |
, ("LANG", "ja_JP.UTF-8") | |
] | |
lookupEnv :: String -> Reader Env String | |
lookupEnv key = do | |
env <- ask | |
return $ fromMaybe "" (lookup key env) | |
test = runReader (lookupEnv "LANG") environment | |
getConfPath :: Reader Env String | |
getConfPath = do | |
env <- ask | |
return $ fromMaybe "/etc" $ lookup "HOME" env | |
-- せっかくだから実際の環境変数を使ってみる | |
main :: IO () | |
main = do | |
env <- getEnvironment | |
putStrLn $ runReader getConfPath env | |
putStrLn $ runReader (lookupEnv "SHELL") env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment