不是Functor(或不可穿越)的Foldable的示例? [英] An example of a Foldable which is not a Functor (or not Traversable)?

查看:98
本文介绍了不是Functor(或不可穿越)的Foldable的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A Foldable instance可能是某种容器,因此也可能是Functor.实际上,

Foldable类型也是一个容器(尽管从技术上讲该类不需要Functor,但有趣的Foldable都是Functor s).

A Foldable type is also a container (although the class does not technically require Functor, interesting Foldables are all Functors).

那么有一个Foldable的例子,它自然不是FunctorTraversable吗? (也许Haskell维基页面可能没有找到:-))

So is there an example of a Foldable which is not naturally a Functor or a Traversable? (which perhaps the Haskell wiki page missed :-) )

推荐答案

下面是一个完全参数化的示例:

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

Weird不是Functor,因为a出现在负位置.

Weird is not a Functor because a occurs in a negative position.

这篇关于不是Functor(或不可穿越)的Foldable的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆