为什么“和[]"是真和“或[]"是假的 [英] Why "and []" is True and "or []" is False
问题描述
为什么空列表上的and"返回true,是否暗示空列表为True?抱歉,我无法正确阅读和理解这一点,所以请纠正我.谢谢.
Why "and" on an empty list returns true, does it imply that an empty list holds True? Sorry but I cannot read and comprehend this correctly, so please correct me. Thanks.
Prelude> and []
True
Prelude> or []
False
推荐答案
在数学中,谈论二元运算通常很有用,例如 &&
, ||
、+
、*
等具有身份.标识是一个值 e
使得以下属性适用于某些通用二进制操作 <>
In mathematics, it's often useful to talk about a binary operation, such as &&
, ||
, +
, *
, etc as having an identity. The identity is a value e
such that the following property holds for some generic binary operation <>
e <> x = x
x <> e = x
对于我上面列出的操作符,它们是可交换的,这意味着 x <>y=y<>x
代表所有的 x
和 y
,所以我们只需要检查上述属性之一.对于and
,有问题的二元运算符是&&
,对于or
,二元运算符是||
.如果我们为这些操作制作一个Cayley 表,它看起来像
For the operators I listed above, they are commutative, meaning that x <> y = y <> x
for all x
and y
, so we only have to check one of the above properties. For and
, the binary operator in question is &&
, and for or
the binary operator is ||
. If we make a Cayley table for these operations, it would look like
&& | False | True
------+-------+------
False | False | False
True | False | True
|| | False | True
------+-------+------
False | False | True
True | True | True
如你所见,对于 &&
如果你有 True &&False
和 True &&对
,答案总是&&
的第二个参数.对于 ||
,如果你有 False ||False
和 False ||对
,答案总是第二个参数,所以每个的第一个参数必须是这些运算符下的标识元素.简单地说:
So as you can see, for &&
if you have True && False
and True && True
, the answer is always the second argument to &&
. For ||
, if you have False || False
and False || True
, the answer is always the second argument, so the first argument of each must be the identity element under those operators. Put simply:
True && x = x
x && True = x
False || x = x
x || False = x
因此,当没有元素可以执行操作时,首选答案是每个操作的标识元素.
Thus, the preferred answer when there are no elements to perform the operator on is the identity element for each operation.
考虑 +
和 *
的标识元素可能会有所帮助,它们是 0
和 1
> 分别:
It might help to also think about the identity elements for +
and *
, which are 0
and 1
respectively:
x + 0 = x = 0 + x
x * 1 = x = 1 * x
您还可以将其扩展到列表连接(++
和 []
)、a -> 类型函数的函数组合等操作.a
((.)
和 id
),以及许多其他的.由于这开始看起来像一种模式,您可能会问这是否已经在 Haskell 中出现,并且确实如此.Data.Monoid
模块定义了抽象这个模式的 Monoid
类型类,它的最小定义是
You can also extend this to operations like list concatenation (++
with []
), function composition for functions of type a -> a
((.)
with id
), along with many others. Since this is starting to look like a pattern, you might ask if this is already a thing in Haskell, and indeed it is. The module Data.Monoid
defines the Monoid
typeclass that abstracts this pattern, and it's minimal definition is
class Monoid a where
mempty :: a -- The identity
mappend :: a -> a -> a -- The binary operator
它甚至将 mappend
别名为 <>
以方便使用(我在上面选择它作为通用二元运算符并不是偶然的).我鼓励您查看该模块并了解其定义.源代码很容易阅读,很有启发性.
And it even aliases mappend
as <>
for ease of use (it was no accident that I choose it above for a generic binary operator). I encourage you to look at that module and play around with its definitions. The source code is quite easy to read and is enlightening.
这篇关于为什么“和[]"是真和“或[]"是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!