如何实现mapAccumM?

时间:2012-07-25 15:06:11

标签: haskell

我希望mapM超过传递累加器时可遍历的内容。我提出了:

import Control.Applicative
import Data.Traversable
import Control.Monad.State

mapAccumM :: (Applicative m, Traversable t, MonadState s m)
             => (s -> a -> m (s, b)) -> s -> t a -> m (t b)
mapAccumM f acc0 xs = put acc0 >> traverse g xs
  where
    g x = do
      oldAcc <- get
      (newAcc, y) <- f oldAcc x
      put newAcc
      return y

如果没有State monad,怎么办呢?

1 个答案:

答案 0 :(得分:2)

roconnor在#haskell

上为我回答了这个问题

这解决了我的问题,但注意到累加器是在元组的第二个元素而不是第一个元素中返回的

mapAccumM :: (Monad m, Functor m, Traversable t) => (a -> b -> m (c, a)) -> a -> t b -> m (t c)
mapAccumM f = flip (evalStateT . (Data.Traversable.traverse (StateT . (flip f))))

或者还要返回累加器:

mapAccumM' :: (Monad m, Functor m, Traversable t) => (a -> b -> m (c, a)) -> a -> t b -> m (t c, a)
mapAccumM' f = flip (runStateT . (Data.Traversable.traverse (StateT . (flip f))))