2013-01-07 20 views
0

我该怎么办才能让put在forM_里面工作在StateT monad上?StateT and forM_

loop :: Integer -> StateT World IO() 
loop passes = do 
    (scene, b1) <- get 
    forM_ [1..passes] $ \pass -> do 
     let b2 = foo b1 pass 
     -- other stuff -- 
     put (scene, b2) -- this no longer puts into the StateT monad 

还是有更好的方法吗?

+0

为什么你不觉得我窝RKS? –

+3

(提示:你的'get'在循环之外,所以只运行*一次*) –

回答

1

我觉得它工作得很好,例如:

--I don't know what World is so I can't use your example 

loop :: StateT Integer IO() 
loop = do 
    forM_ [1..10] put 
    x <- get 
    liftIO $ print x 

运行evalStateT loop 0打印10像我期望的那样。 mapM_引擎盖下做到这一点:

  1. 实际上做的地图,这对于上面的例子返回一个列表[StateT Integer IO()]

  2. 做一个正确的折叠,如:foldr (>>) (return()) listOfMappedValues

目前尚不清楚你究竟期待什么,但是这会让你把事情从mapM_