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

发布于 2024-12-15 05:20:00 字数 174 浏览 2 评论 0原文

我正在通过 LearnYouSomeErlang 网络书学习 Erlang。在学习时令我印象深刻的一件事是非短路布尔合取和析取运算符,即; 。这些运算符的用例是什么?为什么要使用它们而不是 andalsoorelse

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2024-12-22 05:20:00

过去(直到 R13A)andalsoorelse 不是尾递归。有关详细信息,请参阅 http://www.erlang.org/eeps/eep-0017.html。我认为没有充分的理由在新程序中使用 and/or

It used to be (until R13A) that andalso and orelse 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 use and/or in new programs.

多彩岁月 2024-12-22 05:20:00

我认为它们在做不同的事情,并这样使用它们:

  • 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.

女中豪杰 2024-12-22 05:20:00

和/或运算符的历史要悠久得多。 andalso/orelse 运算符是后来添加的。和/或今天的用例可能是当您只想执行一些简单的布尔运算时,并且水平空间比可能节省几个机器周期更重要。例如:

X = Y and (A or B),

而不是

X = Y andalso (A orelse B),

对眼睛更容易一些。

出于向后兼容性的原因,不可能仅更改原始行为和/或变得短路,因此需要新的关键字。名称 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文