0

在 forM_ 中,我怎么能开始put在 StateT monad 上工作?

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 

还是有更好的方法?

4

1 回答 1

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打印。在引擎盖下这样做:10mapM_

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

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

目前尚不清楚您的期望究竟是什么,但这会让您从mapM_

于 2013-01-07T01:23:01.550 回答