Why does application of `sequence` on List of Lists lead to computation of its Cartesian Product?

后端 未结 3 511
死守一世寂寞
死守一世寂寞 2020-12-24 11:58

My question is about the sequence function in Prelude, the signature of which is as follows:

sequence :: Monad m => [m a] ->          


        
3条回答
  •  無奈伤痛
    2020-12-24 12:24

    Just to explain, why the application of sequence to a list of lists is so different from the application of sequence to a list of Maybe-values:

    When you apply sequence to a list of lists, then the type of sequence is specialized from

    sequence :: Monad m => [m a] -> m [a]
    

    to (with the type constructor m set to [])

    sequence :: [[] a] -> [] [a] 
    

    (which is the same as sequence :: [[a]] -> [[a]])

    internally, sequence uses (>>=) -- i.e. the monadic bind function. For lists this bind function is implemented completly different than for m set to Maybe!

提交回复
热议问题