例如,我有某种数据类型。使其成为二叉树:
data Tree a = Leaf a | Branch (Tree a) (Tree a)
例如,我实现了树的遍历:
treeFoldt :: Tree t -> [t]
treeFoldt = foldt1 (:) []
效果很好。但是我想实现Foldable
接口。
我认为,我应该这样写:
instance Foldable Tree where
foldr = treeFoldt
但是它不起作用。我该如何解决?
我可以尝试告诉您代码的问题在哪里,但可惜您没有提供foldt1
的定义>
但是这应该可以工作(如果您对treeFoldt
的实现没问题-请记住:[]
是Foldable
的实例):
instance Foldable Tree where foldr f s = Data.Foldable.foldr f s . treeFoldt
使用
Monoid
的基本定义
无论如何,我认为在这种情况下,最简单的方法是仅实现foldMap
部分:
foldMap
这肯定有效。
import Data.Foldable
import Data.Monoid
data Tree a = Leaf a | Branch (Tree a) (Tree a)
instance Foldable Tree where
foldMap f (Leaf a) = f a
foldMap f (Branch l r) = foldMap f l `mappend` foldMap f r
或
λ> let t = Branch (Branch (Leaf $ Sum 2) (Leaf $ Sum 4)) (Leaf $ Sum 6) λ> fold t Sum {getSum = 12}
当然,您根本不需要
λ> let t = Branch (Branch (Leaf 2) (Leaf 4)) (Leaf 6) λ> foldMap Sum t Sum {getSum = 12}
部分-默认实现就可以了:
Monoid
顺便说一句]:
λ> Data.Foldable.foldr1 (+) t 12
仅仅可以用foldr1 (+)
表示是最不明显的,这是一个不错的(高级)练习,尝试自己完成:D
[我认为foldMap
通常是关于Foldable and Traversable by A. Arnold(和Foldable
)的不错的博客文章-也许您也觉得它很有帮助