Erlang 中非短路布尔运算符有什么用?
我正在通过 LearnYouSomeErlang 网络书学习 Erlang。在学习时令我印象深刻的一件事是非短路布尔合取和析取运算符,即; 和
和或
。这些运算符的用例是什么?为什么要使用它们而不是 andalso
和 orelse
?
I am learning Erlang from the LearnYouSomeErlang web-book. One thing that struck me while learning was the non short-circuiting boolean conjunction and disjunction operators viz; and
and or
. What are the use cases for these operators? Why would you want to use them instead of andalso
and orelse
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
过去(直到 R13A)
andalso
和orelse
不是尾递归。有关详细信息,请参阅 http://www.erlang.org/eeps/eep-0017.html。我认为没有充分的理由在新程序中使用and
/or
。It used to be (until R13A) that
andalso
andorelse
weren't tail recursive. See http://www.erlang.org/eeps/eep-0017.html for details. I don't think there is a good reason to useand
/or
in new programs.我认为它们在做不同的事情,并这样使用它们:
and
/or
作为逻辑运算符,我想在其中比较逻辑值。由于它们很严格,我会自动进行类型检查,并且我知道确切地调用了什么。andalso
/orelse
用于控制,很像 C 中的&&
和||
。查看错误的定义在 erlang 中,我觉得很高兴知道已经执行了什么以及它是如何执行的。
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. As they are strict I automatically get type-checking and I KNOW exactly what has been called.andalso
/orelse
for control, much like&&
and||
in C.Seeing errors are defined in erlang I feel it is good to know what has been executed and how it went.
和/或运算符的历史要悠久得多。 andalso/orelse 运算符是后来添加的。和/或今天的用例可能是当您只想执行一些简单的布尔运算时,并且水平空间比可能节省几个机器周期更重要。例如:
而不是
对眼睛更容易一些。
出于向后兼容性的原因,不可能仅更改原始行为和/或变得短路,因此需要新的关键字。名称 andalso/orelse 来自标准 ML。
The and/or operators are simply much older. The andalso/orelse operators are later additions. 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:
rather than
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.