简体   繁体   English

在Erlang中使用非短路布尔运算符有什么用?

[英]What is the use of non short-circuiting boolean operators in Erlang?

I am learning Erlang from the LearnYouSomeErlang web-book. 我正在从LearnYouSomeErlang网络书籍学习Erlang。 One thing that struck me while learning was the non short-circuiting boolean conjunction and disjunction operators viz; 学习过程中让我感到震惊的一件事是非短路布尔连接和分离算子即可; and and or . andor What are the use cases for these operators? 这些运营商的用例是什么? Why would you want to use them instead of andalso and orelse ? 你为什么要使用它们,而不是andalsoorelse

It used to be (until R13A) that andalso and orelse weren't tail recursive. 它使用的是(直到R13A),该andalsoorelse不是尾递归。 See http://www.erlang.org/eeps/eep-0017.html for details. 有关详细信息,请参见http://www.erlang.org/eeps/eep-0017.html I don't think there is a good reason to use and / or in new programs. 我不认为有充分的理由使用and / or新的程序中。

I see them as doing different things and use them as such: 我认为他们做了不同的事情并使用它们:

  • and / or as logical operators where I want to compare the logical values. and / or作为逻辑运算符,我想比较逻辑值。 As they are strict I automatically get type-checking and I KNOW exactly what has been called. 由于它们很严格,我会自动进行类型检查,而且我知道所谓的内容。
  • andalso / orelse for control, much like && and || andalso / orelse控制,就像&&|| in C. 在C.

Seeing errors are defined in erlang I feel it is good to know what has been executed and how it went. 看到错误是在erlang中定义的,我觉得知道已经执行了什么以及它是如何运行的。

The and/or operators are simply much older. 和/或操作员只是更老了。 The andalso/orelse operators are later additions. andalso / orelse运算符是后来添加的。 A use case for and/or today could be when you just want to perform some simple boolean operations and horizontal space is more important than possibly saving a couple of machine cycles. 用于和/或今天的用例可能是您只想执行一些简单的布尔操作,而水平空间比可能节省几个机器周期更重要。 For example: 例如:

X = Y and (A or B),

rather than 而不是

X = Y andalso (A orelse B),

is a bit easier on the eyes. 眼睛有点容易。

For reasons of backwards compatibility, it wasn't possible to just change the behaviour of the original and/or to become short-circuiting, so new keywords were needed. 出于向后兼容性的原因,不可能只改变原始行为和/或变为短路,因此需要新的关键字。 The names andalso/orelse come from Standard ML. 名称和其他/ orelse来自标准ML。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何修改运算符“&&”和“||”的定义使他们不使用短路评估? - How would I modify the definition of the operators "&&" and "||" so that they do not use short circuit evaluation? 除IEEE-754浮点类型外,哪些类型非透明地定义关系运算符或非反身地定义相等性 - What types other than IEEE-754 floating-point types define relational operators non-transitively or equality non-reflexively 为什么C#不接受布尔运算符作为当前运算符的单词(“and”,“或”,“not”,...等)? - Why C# doesn't accept the boolean operators as words (“and”, “or”, “not” ,..etc) with the current operators? 在Python中实现运算符优先级的一般方法是什么? - What is the general way to implement operators precedence in Python 什么是非本地退货? - What is a non-local return? “布尔”一词与计算机有关的含义是什么? - What is the meaning of the word “Boolean” as it relates to computers? 复合赋值运算符,如果修改了值(同时)会发生什么? - Compound assignment operators, what happens if the value is modified (in the meanwhile)? 为什么编程语言不使用简化的布尔表达式? - Why don't programming languages use simplified boolean expressions? erlang中的静态类型检查 - Static typechecking in erlang 像Coq这样的非图灵完整语言有哪些实际限制? - What are the practical limitations of a non-turing complete language like Coq?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM