当前位置:  开发笔记 > 编程语言 > 正文

实现`traverse`和`sequenceA`

如何解决《实现`traverse`和`sequenceA`》经验,为你挑选了1个好方法。

我正在努力实现traversesequenceA独立实施:

这是我traverse'在以下方面的实施sequenceA:

traverse' :: (Functor t, Foldable t, Applicative f) => (a -> f b) -> t a -> f (t b)
traverse' f x = sequenceA' $ fmap f x 

但是,我坚持执行sequenceA.

sequenceA' :: (Functor t, Foldable t, Applicative f) => t (f a) -> f (t a)
sequenceA' x = foldr (\a _ -> fmap helper a) (error "...") x

我使用了一个占位符b(error "...") - 我不知道如何创建b给定的这些类型.

helper :: (Functor t, Foldable t) => a -> t a
helper x = error "TODO"

此外,我使用该helper函数来获取此代码进行类型检查.但是,再次,这只是一个error电话.请给我一个如何一般实施的提示sequenceA.



1> hao..:

鉴于:

traverse' :: (Functor t, Foldable t, Applicative f) => (a -> f b) -> t a -> f (t b)
traverse' a2fb ta = sequenceA' (fmap a2fb ta)

sequenceA' :: (Functor t, Foldable t, Applicative f) => t (f a) -> f (t a)
sequenceA' tfa = _

洞的理想用途!我们为所有参数命名(不需要进行无点操作,让我们的生活变得非常困难)并在定义应该是一个洞.这会产生:

src/Main.hs:8:14: Found hole ‘_’ with type: f (t a) …

我们现在转向一个返回类似函数的函数f (t a).幸运的traverse'是,现在我们可以改进我们的洞:

sequenceA' :: (Functor t, Foldable t, Applicative f) => t (f a) -> f (t a)
sequenceA' tfa = traverse _ _

src/Main.hs:8:27: Found hole ‘_’ with type: a0 -> f a …
src/Main.hs:8:29: Found hole ‘_’ with type: t a0 …

第一个看起来很乱,但我们知道的东西适合t a0第二个,这是我们的tfa.插头和突突:

sequenceA' :: (Functor t, Foldable t, Applicative f) => t (f a) -> f (t a)
sequenceA' tfa = traverse _ tfa

src/Main.hs:8:27: Found hole ‘_’ with type: f a -> f a …

好吧,我们知道具有该签名的功能,那就是id.所以我们的最终答案是:

traverse' :: (Functor t, Foldable t, Applicative f) => (a -> f b) -> t a -> f (t b)
traverse' a2fb ta = sequenceA' (fmap a2fb ta)

sequenceA' :: (Functor t, Foldable t, Applicative f) => t (f a) -> f (t a)
sequenceA' tfa = traverse id tfa

我们现在可以变得年轻,狂野,无趣(-ish):

traverse' :: (Functor t, Foldable t, Applicative f) => (a -> f b) -> t a -> f (t b)
traverse' a2fb = sequenceA' . fmap a2fb

sequenceA' :: (Functor t, Foldable t, Applicative f) => t (f a) -> f (t a)
sequenceA' = traverse id

这与Prelude中的定义相匹配.〜〜孔

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有