2014-08-31 56 views
3

下面是我的程序構造的單子棧:提供測試執行單子疊

type Px a = ReaderT PConf (State PState) a 

凡PConf和PState是抱着配置和應用程序的狀態,任意數據類型。

我需要提供一個默認的實現,所以我可以測試我的功能,賦予了「PConf」和「PState」:

p4 :: Px() 
p4 = ??? 
    where 
    conf = PConf 4 10 10 [0, 1] 
    state = PState 0 0 $ M.fromList [((x, y), Nothing) | x <- [0..9], y <- [0..9]] 

我不知道在「P4 =」寫的那麼該實現使用'conf'和'state'。 謝謝。

回答

4

如果p4Px()動作,然後就是點你定義conf傳中,如果state應該是初始狀態,而不是一個無論是。相反,你應該把那些在功能測試p4

p4 :: Px() 
p4 = ... 

test = runState (runReaderT p4 conf) state 
    where 
    conf = PConf 4 10 10 [0, 1] 
    state = PState 0 0 $ M.fromList [((x, y), Nothing) | x <- [0..9], y <- [0..9]] 

編輯:如果您的問題仍與寫作p4本身,這裏是一個小樣機p4改變一些國家的遏制一些原件conf

p4 = do 
    PConf x y z _ <- ask 
    PState m n mp <- get 
    put $ PState m n (M.insert (x, y) (Just z) mp)