为什么“和”[]“是真的并且“或[]”是假的 [英] Why "and []" is True and "or []" is False

查看:130
本文介绍了为什么“和”[]“是真的并且“或[]”是假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么和在空列表中返回true,是否意味着空列表为True?对不起,但我无法正确阅读和理解,请纠正我。谢谢。

  Prelude>和[] 
True
Prelude>或[]
False


解决方案

讨论二进制操作通常很有用,如&& || + * 等具有身份。该身份是一个值 e ,因此以下属性适用于某些通用二元操作<>

  e<> x = x 
x<> e = x

对于上面列出的操作符,它们是可交换的,这意味着 x<> y = y<所有 x y 都是x ,所以我们只需要检查上面的一个属性。对于,二元运算符是&& ,对于二元运算符是 || 。如果我们为这些操作制作 Cayley表,它将看起来像

 &安培;&安培; | False |真
------ + ------- + ------
False | False | False
True | False |真


|| | False |真
------ + ------- + ------
False | False |真
True | True |真正的

正如你所看到的,对于&& 如果您有 True&& False True&&真,答案始终是&& 的第二个参数。对于 || ,如果您有 False || False False || True ,答案总是第二个参数,因此每个参数的第一个参数必须是这些操作符下的标识元素。简单地说:

  True&& x = x 
x&& True = x

False || x = x
x || False = x

因此,当没有要执行操作符的元素时,首选答案是身份元素用于每个操作。




也可以考虑 + * ,它们是 0 1

  x + 0 = x = 0 + x 
x * 1 = x = 1 * x

您也可以将其扩展为列表级联( ++ [] ),函数的组合函数类型为 a - >一个(。) id )以及其他许多内容。由于这开始看起来像一个模式,你可能会问这是否已经是Haskell中的事情了,事实上它确实如此。模块 Data.Monoid 定义了抽象这个模式的 Monoid 类型类,它的最小定义是

  class Monoid a where 
mempty :: a - 身份
mappend :: a - > a - > a - 二元运算符

甚至可以使用别名 mappend 作为<> ,以方便使用(我在上面为通用二元运算符选择它并非偶然)。我鼓励你看看这个模块,并发挥它的定义。源代码很容易阅读并且很有启发性。

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

解决方案

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

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

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.


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

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

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屋!

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