mapM和forM

时间:2016-04-02 10:16:02

标签: haskell monads

您能告诉我mapMforM之间是否存在一些差异? 看签名:

mapM :: Monad m => (a -> m b) -> [a] -> m [b]
forM :: Monad m => [a] -> (a -> m b) -> m [b]

唯一的问题是参数的顺序。这是不可取的,但它真的只是区别吗?

1 个答案:

答案 0 :(得分:5)

forMbase中定义为flip mapM

-- | 'forM' is 'mapM' with its arguments flipped. For a version that
-- ignores the results see 'Data.Foldable.forM_'.
forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
{-# INLINE forM #-}
forM = flip mapM