Skip to content

Instantly share code, notes, and snippets.

@krdlab
Created July 18, 2012 17:36
Show Gist options
  • Save krdlab/3137647 to your computer and use it in GitHub Desktop.
Save krdlab/3137647 to your computer and use it in GitHub Desktop.
practice: Control.Monad.Reader
module Main where
import Control.Monad.Reader
{-
(1) local 関数の例
local :: (r -> r) -> m a -> m a
r は Reader Monad が伝搬する読み取り値の型
-}
myName step = do
name <- ask
return $ step ++ ", I am " ++ name
localExample :: Reader String (String, String, String)
localExample = do
a <- myName "First"
b <- local (++ "dy") $ myName "Second"
c <- myName "Third"
return (a, b, c)
{-
(2) ユーザ定義型の例
-}
data MyConf = MyConf {
getString :: String
, getInt :: Int
} deriving (Show)
myApp :: Reader MyConf (String, Int)
myApp = do
conf <- ask
return (getString conf, getInt conf)
{- 実行例
*Main> runReader localExample "HOGE"
("First, I am HOGE","Second, I am HOGEdy","Third, I am HOGE")
*Main> runReader myApp $ MyConf "HOGE" 777
("HOGE",777)
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment