您能告诉我mapM
和forM
之间是否存在一些差异?
看签名:
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
forM :: Monad m => [a] -> (a -> m b) -> m [b]
唯一的问题是参数的顺序。这是不可取的,但它真的只是区别吗?
答案 0 :(得分:5)
forM
在base
中定义为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