【发布时间】:2017-09-02 21:46:49
【问题描述】:
我试图从Applicative 接口了解究竟需要什么才能执行任何traverse。我被卡住了,因为它们没有在默认实现中使用,就好像约束是严格的一样。 Haskell 的类型系统是否太弱,无法描述实际需求?
-- | Map each element of a structure to an action, evaluate these actions
-- from left to right, and collect the results. For a version that ignores
-- the results see 'Data.Foldable.traverse_'.
traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
traverse f = sequenceA . fmap f
-- | Evaluate each action in the structure from left to right, and
-- and collect the results. For a version that ignores the results
-- see 'Data.Foldable.sequenceA_'.
sequenceA :: Applicative f => t (f a) -> f (t a)
sequenceA = traverse id
一个可能相关的附带问题,为什么在Foldable 中定义sequenceA_?
【问题讨论】:
-
第二个问题似乎与第一个问题完全无关。但答案很简单:
Foldable足够强大,可以实现sequenceA_。
标签: haskell