Skip to content

Instantly share code, notes, and snippets.

@apstndb
Last active December 11, 2015 09:58
Show Gist options
  • Save apstndb/4583145 to your computer and use it in GitHub Desktop.
Save apstndb/4583145 to your computer and use it in GitHub Desktop.
第7回 スタートHaskell2演習問題「Readerモナドを使ってみよう」解答 http://wiki.haskell.jp/Workshop/StartHaskell2/exercise14
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