Erlang 相当于 if else

发布于 2024-10-05 02:26:08 字数 386 浏览 3 评论 0原文

我有两部分代码想要执行。两者都是条件条件,

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

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

发布评论

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

评论(4

绿光 2024-10-12 02:26:09

首先,我建议您习惯使用“case”语句,因为“if”条件仅限于保护表达式:

case custom_call(A) of
  1 -> do1(A);
  2 -> do2(A)
end.

除了“if”和“case”之外,还有一种从 R13 开始可以进行条件执行的方法:

  1> N =10.
  10
  2> ((N > 10) andalso more).      
  false
  3> ((N == 10) andalso equals).
  equals

First of all, I recommend you to get used to use 'case' statement, because of 'if' conditions restricted to guard expressions:

case custom_call(A) of
  1 -> do1(A);
  2 -> do2(A)
end.

There is one more way to do conditional execution besides 'if' and 'case' that works starting from R13:

  1> N =10.
  10
  2> ((N > 10) andalso more).      
  false
  3> ((N == 10) andalso equals).
  equals
ˇ宁静的妩媚 2024-10-12 02:26:08

if 的形式是:

if
    <guard 1> -> <body1> ;
    <guard 2> -> <body2> ;
    ...
end

它按照自上而下的顺序尝试 if 子句中的防护(这是定义的),直到达到成功的测试,然后评估该子句的主体并if 表达式返回主体中最后一个表达式的值。因此其他语言中的else位被融入其中。如果没有一个守卫成功,则会生成 if_clause 错误。常见的包罗万象的保护只是 true ,它总是成功,但包罗万象可以是任何真实的东西。

case 的形式是:

case <expr> of
    <pat 1> -> <body1> ;
    <pat 2> -> <body2> ;
    ...
end

它的工作原理是首先评估,然后尝试将该值与 case 子句中的模式按照 op-down 顺序(这是已定义的)进行匹配,直到匹配,然后是正文该条款的内容是
求值,case 表达式返回主体中最后一个表达式的值。如果没有模式匹配,则会生成 case_clause 错误。

请注意,ifcase 都是表达式(一切都是表达式),因此它们都必须返回值。这就是为什么如果没有成功/匹配则没有默认值的原因之一。还强迫你涵盖所有选项;这对于case尤其重要。 if 只是 case 的退化情况,因此它继承了它。 Erlang Rationale 中有一些 if 的历史,您可以在 trapexit.org 的用户贡献下找到它。

The form for an if is:

if
    <guard 1> -> <body1> ;
    <guard 2> -> <body2> ;
    ...
end

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 the else bit in other languages is baked into it. If none of the guards succeeds then an if_clause error is generated. A common catch-all guard is just true which always succeeds, but a catch-all can be anything which is true.

The form for a case is:

case <expr> of
    <pat 1> -> <body1> ;
    <pat 2> -> <body2> ;
    ...
end

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 a case_clause error is generated.

Note that if and case 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 for case. if is just a degenerate case of case so it inherited it. There is a bit of history of if in the Erlang Rationale which you can find on trapexit.org under user contributions.

只等公子 2024-10-12 02:26:08

Erlang 不允许您使用没有 true 语句选项的 if 语句。这是一个真实的陈述还是一个实际的 true 取决于您,但让您的 true 成为 else 是很常见的。代码> 其他语言。

if 
    some_condition -> some_code;
    some_other_condition -> some_other_code;
    true -> else_code
end.

请参阅“如果会怎样?”有关详细信息,请参阅页面上的部分。

Erlang doesn't allow you to have an if without a true statement option. Whether or not this is something that is a true statement or an actual true is up to you, but it is commonplace to have your true be the else in other languages.

if 
    some_condition -> some_code;
    some_other_condition -> some_other_code;
    true -> else_code
end.

See the "What the If?" section on this page for more on this.

灯下孤影 2024-10-12 02:26:08

请记住 Erlang 中的 if 有一个要返回的值,而且它是一个表达式。这与 C 或 Java 中的 if 不同。

如果您想对某个值执行某些操作,代码应该如下所示;

if
  % do something and get the value
  X >= Val -> Something;
  % for doing something otherwise and get the value
  true -> Else_than_the_Something 
end.

请参阅Erlang 参考手册的 if 表达式部分 了解更多详细信息。

Remember if in Erlang has a value to return, and it's an expression. It's not that if like in C or Java.

If you want to do something for a value, the code should be something like this;

if
  % do something and get the value
  X >= Val -> Something;
  % for doing something otherwise and get the value
  true -> Else_than_the_Something 
end.

See Section for the if expression of Erlang Reference Manual for the further details.

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