不是函子(或不可遍历)的可折叠示例? [英] An example of a Foldable which is not a Functor (or not Traversable)?

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

问题描述

A 可折叠 实例可能是某种容器,因此也可能是 Functor.事实上,这个

一个Foldable类型也是一个容器(虽然这个类在技术上不需要Functor,有趣的Foldable都是Functors).

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

那么有没有一个 Foldable 的例子,它不是一个 Functor 或一个 Traversable?(可能是 Haskell 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 :-) )

推荐答案

这是一个完全参数化的例子:

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.

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

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