[英]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
. and
和or
。 What are the use cases for these operators? 这些运营商的用例是什么? Why would you want to use them instead of
andalso
and orelse
? 你为什么要使用它们,而不是
andalso
和orelse
?
It used to be (until R13A) that andalso
and orelse
weren't tail recursive. 它使用的是(直到R13A),该
andalso
和orelse
不是尾递归。 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. 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.