>> =和concatMap之间的区别

时间:2017-04-12 19:24:33

标签: haskell monads

我今天正在玩>>=,试图了解monad,并找到了一个有趣的模式。使用列表monad时,>>=似乎表现得像concatMap。我四处寻找,试图找到任何相似之处,特别是在hackage的定义中。

我尝试过的一些事情:

[1, 2, 3] >>= (iter 5 id) => [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3]

concatMap (iter 5 id) [1, 2, 3]=> [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3]

[1, 2, 3] >>= (iter 5 (+5)) => [1,6,11,16,21,2,7,12,17,22,3,8,13,18,23]

concatMap (iter 5 (+5) ) [1, 2, 3] => [1,6,11,16,21,2,7,12,17,22,3,8,13,18,23]

iter只是非无限迭代,

iter i f a = toL $ Data.Sequence.iterateN i f a
  where
    toL = Data.Foldable.toList :: Data.Sequence.Seq a -> [a] 

(在repl.it中工作,因此搞砸了导入)。

列表的(>>=)是否等同于concatMap? 它是concatMap的推广吗?

2 个答案:

答案 0 :(得分:9)

是的,论点被翻转了。比较这些类型的签名应该会带来相似性,尽管特殊列表语法会干扰:

Prelude> :t flip concatMap
flip concatMap :: Foldable t => t a -> (a -> [b]) -> [b]
Prelude> :t (>>=)
(>>=)          :: Monad m    => m a -> (a -> m b) -> m b

答案 1 :(得分:7)

是的,列表(=<<)concatMap。对于任何Monad,>>=只是fmapjoinconcat的推广)的组合,所以这也是直觉上的。