Erlang 相当于 if else
我有两部分代码想要执行。两者都是条件条件,
if Value1 < N do something
else if Value1 >= N do something
if Value2 < N do something
else if Value2 >= N do something
我希望每个语句都有一个执行。
erlang 中的 if 是如何工作的?没有别的了。我使用了多个守卫,但看起来我有 4 个 if 语句。 2 人一组。
if some condition
code;
if other condition
code
end.
我收到语法错误。
I have 2 parts of code I want to execute. Both are conditionals
if Value1 < N do something
else if Value1 >= N do something
if Value2 < N do something
else if Value2 >= N do something
I want at one statement of each to execute.
How does the if work in erlang? there is no else. I use multiple guards, but that looks like I have 4 if statements. in groups of 2.
if some condition
code;
if other condition
code
end.
I get a syntax error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,我建议您习惯使用“case”语句,因为“if”条件仅限于保护表达式:
除了“if”和“case”之外,还有一种从 R13 开始可以进行条件执行的方法:
First of all, I recommend you to get used to use 'case' statement, because of 'if' conditions restricted to guard expressions:
There is one more way to do conditional execution besides 'if' and 'case' that works starting from R13:
if
的形式是:它按照自上而下的顺序尝试 if 子句中的防护(这是定义的),直到达到成功的测试,然后评估该子句的主体并
if
表达式返回主体中最后一个表达式的值。因此其他语言中的else
位被融入其中。如果没有一个守卫成功,则会生成if_clause
错误。常见的包罗万象的保护只是true
,它总是成功,但包罗万象可以是任何真实的东西。case
的形式是:它的工作原理是首先评估,然后尝试将该值与 case 子句中的模式按照 op-down 顺序(这是已定义的)进行匹配,直到匹配,然后是正文该条款的内容是
求值,
case
表达式返回主体中最后一个表达式的值。如果没有模式匹配,则会生成case_clause
错误。请注意,
if
和case
都是表达式(一切都是表达式),因此它们都必须返回值。这就是为什么如果没有成功/匹配则没有默认值的原因之一。还强迫你涵盖所有选项;这对于case
尤其重要。if
只是case
的退化情况,因此它继承了它。 Erlang Rationale 中有一些if
的历史,您可以在 trapexit.org 的用户贡献下找到它。The form for an
if
is:It works trying the guards in if-clauses in top-down order (this is defined) until it reaches a test which succeeds, then the body of that clause is evaluated and the
if
expression returns the value of the last expression in the body. So theelse
bit in other languages is baked into it. If none of the guards succeeds then anif_clause
error is generated. A common catch-all guard is justtrue
which always succeeds, but a catch-all can be anything which is true.The form for a
case
is:It works by first evaluating and then trying to match that value with patterns in the case-clauses in op-down order (this is defined) until one matches, then the body of that clause is
evaluated and the
case
expression returns the value last expression in the body. If no pattern matches then acase_clause
error is generated.Note that
if
andcase
are both expressions (everything is an expression) so they both must return values. That is one reason why there is no default value if nothing succeeds/matches. Also to force you to cover all options; this is especially important forcase
.if
is just a degenerate case ofcase
so it inherited it. There is a bit of history ofif
in the Erlang Rationale which you can find on trapexit.org under user contributions.Erlang 不允许您使用没有
true
语句选项的if
语句。这是一个真实的陈述还是一个实际的true
取决于您,但让您的true
成为else
是很常见的。代码> 其他语言。请参阅“如果会怎样?”有关详细信息,请参阅此页面上的部分。
Erlang doesn't allow you to have an
if
without atrue
statement option. Whether or not this is something that is a true statement or an actualtrue
is up to you, but it is commonplace to have yourtrue
be theelse
in other languages.See the "What the If?" section on this page for more on this.
请记住 Erlang 中的
if
有一个要返回的值,而且它是一个表达式。这与 C 或 Java 中的 if 不同。如果您想对某个值执行某些操作,代码应该如下所示;
请参阅Erlang 参考手册的
if
表达式部分 了解更多详细信息。Remember
if
in Erlang has a value to return, and it's an expression. It's not thatif
like in C or Java.If you want to do something for a value, the code should be something like this;
See Section for the
if
expression of Erlang Reference Manual for the further details.