Haskell:一个不是Functor(或不可穿越)的Foldable的例子? [英] Haskell : An example of a Foldable which is not a Functor (or not Traversable)?
问题描述
可折叠
实例很可能是某种容器,所以很可能也是 Functor
。的确,这个说:
A Foldable
instance is likely to be some sort of container, and so is likely to be a Functor
as well. Indeed, this says
A
Foldable
type也是一个容器(尽管这个类在技术上并不需要Functor
,有趣的Foldable
s都是Functor
s)。
A
Foldable
type is also a container (although the class does not technically requireFunctor
, interestingFoldable
s are allFunctor
s).
那么是否存在一个 Foldable
的例子,它本质上不是一个 Functor
或一个 Traversable的
? (这可能是哈斯克尔的wiki页面错过了:-))
So is there an example of a Foldable
which is not naturally a Functor
or a Traversable
? (which perhaps the Haskell wiki page missed :-) )
推荐答案
这是一个完全参数化的例子: b
Here's a fully parametric example:
data Weird a = Weird a (a -> a)
instance Foldable Weird where
foldMap f (Weird a b) = f $ b a
奇怪的
是不是 Functor
,因为 a
出现在负数位置。
Weird
is not a Functor
because a
occurs in a negative position.
这篇关于Haskell:一个不是Functor(或不可穿越)的Foldable的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!