为什么`mappend`中缀不是`&`而不是`+`? [英] Why is the `mappend` infix alias `<>` instead of `+`?

查看:378
本文介绍了为什么`mappend`中缀不是`&`而不是`+`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Haskell中,为什么中缀别名 mappend (来自类 Monoid <>< / code>而不是 + ?在代数课程中, + 通常用于幺半群的二元运算符。

In Haskell, why is the infix alias of mappend (from class Monoid) <> instead of +? In algebra courses + is usually used for the binary operator of a monoid.

推荐答案

+ 函数是特定于数字的,此外,它只是实现数字的Monoid的一种方法( * 同样有效)。同样,使用布尔值,使用&& || 也是同样有效的。使用符号 + 表明Monoid是关于 addition 的,具体来说,它们实际上就是关于任何关联操作

The function + is specific to numbers, and moreover, it's only one way to implement Monoid for numbers (* is equally valid). Similarly, with booleans, it would be equally valid to use && and ||. Using the symbol + suggests that Monoids are about addition specifically, when really they're just about any associative operation.

确实,至少在我的经验中,可能会以类似加法的方式使用 mappend :连接列表或向量,接受集合或地图的联合等等。然而,哈斯克尔的思维倾向于普遍性和坚持数学原理(可以说)更直观。在我看来,把 mappend 看作是一种普遍的加法是合理的,并且在它不是。

It is true that, at least in my experience, one is likely to use mappend in a fashion resembling addition: concatenating lists or vectors, taking unions of sets or maps, etc, etc. However, the Haskell mindset favors generality and adherence to mathematical principles over (arguably) what is more intuitive. It's certainly reasonable, in my opinion, to think of mappend as a sort of general addition, and make adjustments in the cases where it isn't.

这篇关于为什么`mappend`中缀不是`&`而不是`+`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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