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

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

问题描述

可折叠 实例很可能是某种容器,所以很可能也是 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 require Functor, interesting Foldables are all Functors).

那么是否存在一个 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屋!

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