为什么大多数编程语言只具有二进制相等比较运算符? [英] Why do most programming languages only have binary equality comparison operators?

查看:93
本文介绍了为什么大多数编程语言只具有二进制相等比较运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在自然语言中,我们会说如果颜色是红色,蓝色或黄色,则某些颜色是原色."

In natural languages, we would say "some color is a primary color if the color is red, blue, or yellow."

在我所见过的每种编程语言中,它都可以翻译为:

In every programming language I've seen, that translates into something like:

isPrimaryColor = someColor == "Red" or someColor == "Blue" or someColor == "Yellow"

为什么没有一种语法与英语句子更加匹配.毕竟,您不会说如果某种颜色是红色,或者该颜色是蓝色,或者该颜色是黄色,那么某些颜色就是原色."

Why isn't there a syntax that more closely matches the English sentence. After all, you wouldn't say "some color is a primary color if that color is red, or that color is blue, or that color is yellow."

我简单地认识到isPrimaryColor = someColor == ("Red" or "Blue" or "Yellow"),因为它们可能不是布尔值,而是红色和蓝色,在这种情况下,布尔逻辑适用,但是诸如此类:

I realize simply isPrimaryColor = someColor == ("Red" or "Blue" or "Yellow") because instead of Red Blue and Yellow they could be boolean statement in which case boolean logic applies, but what about something like:

isPrimaryColor = someColor ( == "Red" or == "Blue" or == "Yellow")

要使语法具有更大的灵活性,这是一个额外的好处,例如,您想查看一个数字是介于1到100还是1000到2000之间,您可以说:

As an added bonus that syntax would allow for more flexibility, say you wanted to see if a number is between 1 and 100 or 1000 and 2000, you could say:

someNumber ((>= 1 and <=100) or (>=1000 and <=2000))

非常有趣的答案,并指出我应该学习更多的语言.阅读完答案后,我同意对于严格相等比较而言,类似于集合成员资格的事物是表达同一件事的一种清晰明了的方式(对于支持简洁内联列表或集合以及测试成员资格的语言的语言)

Very interesting answers, and point taken that I should learn more languages. After reading through the answers I agree that for strictly equality comparison something similar to set membership is a clear and concise way of expressing the same thing (for languages that have language support for concise inline lists or sets and testing membership)

出现的一个问题是,如果要比较的值是昂贵的计算结果,则需要(应该)创建一个临时变量.另一个问题是,可能需要检查不同的评估结果,例如某些昂贵的计算结果应为质数,且应介于200到300之间"

One issues that came up is that if the value to compare is the result of an expensive calculation a temporary variable would need to be (well, should be) created. The other issue is that there may be different evaluations that need to be checked, such as "the result of some expensive calculation should be prime and between 200 and 300"

这些功能场景也被更多的功能性语言(尽管取决于语言可能不那么简洁)或实际上可以将功能作为参数的任何语言所涵盖.例如,前面的示例可能是

These scenarios are also covered by more functional languages (though depending on the language may not be more concise), or really any language that can take a function as a parameter. For instance the previous example could be

MeetsRequirements(GetCalculatedValue(), f(x):x > 200, f(x):x < 300, IsPrime)

推荐答案

在Haskell中,很容易定义一个函数来执行此操作:

In Haskell, it is easy to define a function to do this:

matches x ps = foldl (||) False $  map (\ p -> p x) ps

此函数获取谓词的值列表(类型为a -> Bool),如果任何谓词与该值匹配,则返回True.

This function takes a value list of predicates (of type a -> Bool) and returns True if any of the the predicates match the value.

这允许您执行以下操作:

This allows you to something like this:

isMammal m = m `matches` [(=="Dog"), (=="Cat"), (=="Human")]

有趣的是,它不必只是相等的,您可以使用任何具有正确类型的东西:

The nice thing is that it doesn't have to just be equality, you can use anything with the correct type:

isAnimal a = a `matches` [isMammal, (=="Fish"), (=="Bird")]

这篇关于为什么大多数编程语言只具有二进制相等比较运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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