如何写“a == b? X:Y“在Erlang? [英] How to write "a==b ? X : Y" in Erlang?

查看:362
本文介绍了如何写“a == b? X:Y“在Erlang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  A == B是否有一个很好的方式在Erlang中编写这样的代码? X:Y 

下面是ruby样式的代码

解决方案

说明



三元运算符 _? _:_ 以许多语言存在是由于它们有两个句法类:语句和表达式。因为if-then-else结构通常属于statement-class,所以当你输入一个表达式时,没有办法让它工作。因此,您添加 _? _:_ 运算符到表达式类。



作为另一个帖子,你可以采取 a == b? true:false 并且只写 a == b ,但这并不能解释一般情况,我们可能会有 a == b? X:Y 用于任意表达式 X Y 。另外请注意,Erlang中的 a == b 始终是 false ,所以你可以认为真正的事情是以 false 替换整个表达式。



幸运的是,Erlang与大多数功能语言一样,具有一个句法类,表达式。因此,您可以使用案例a == b的X - > ...; Y - > ...结束在函数中的任何位置,其他表达式包括。换句话说,三元 _? _ $ _ code>操作符在Erlang中是多余的,因为 case 已经有效。



例如:



假设我们要返回一个简单的proplist,我们有一些计算我们需要做的

  f() - > 
case a == b of
true - >
[{a,3},
{b,<YE>},
{c,7}];
false - >
[{a,3},
{b,<YE>},
{c,HELLO !!!}]
结束。

但由于案例我们可以把它列入:

  f() - > 
[{a,3},
{b,<YE>},
{c,case a == b of
true - > 7;
false - > 你好!!!
end}]。

并完成此事。



为什么我不主张使用IF



if .. end Erlang中的构建通常不是什么你要。在这种情况下,您要检查一个值 a == b ,它可以产生两个输出之一 true 。在这种情况下, case -expression更直接。如果您必须检查多个不同的测试并选择第一个匹配项,那么如果更好地使用,而我们只能在此进行一次测试。


Is there a good way to write code like this in Erlang ?

A == B ? X : Y

below is ruby-style code

解决方案

Explanation

The reason the ternary operator _ ? _ : _ exists in many languages is due to the fact that they have two syntactic classes: Statements and Expressions. Since if-then-else constructions usually belong the the statement-class, there is no way to get that working for when you are entering an expression. Hence you add the _ ? _ : _ operator to the expression class.

As another post states, you can take a == b ? true : false and just write a == b, but that does not explain the general case where we may have a == b ? X : Y for arbitrary expressions X and Y. Also note that a == b is always false in Erlang, so you could argue that the real thing to do is to replace the whole expression with false.

Luckily, Erlang, as is the case for most functional languages, have one syntactic class only, expressions. Hence you can use case a == b of X -> ...; Y -> ... end in any place in a function, other expressions included. In other words, the ternary _ ? _ : _ operator is redundant in Erlang since the case already works.

An example:

Suppose we are to return a simple proplist and we have some computation we need to do

  f() ->
    case a == b of
          true -> 
           [{a, 3},
            {b, <<"YE">>},
            {c, 7}];
          false ->
           [{a, 3},
            {b, <<"YE">>},
            {c, "HELLO!!!"}];
    end.

But since the case construction is an expression, we can just inline it:

  f() ->
    [{a, 3},
     {b, <<"YE">>},
     {c, case a == b of
          true -> 7;
          false -> "HELLO!!!"
         end}].

and be done with the thing.

Why I am not advocating the use of IF

the if .. end construction in Erlang is usually not what you want. You want to scrutinize a value a == b in this case and it can yield one of two outputs true or false. In that case the case-expression is more direct. The if is better used if you have to check for multiple different tests and pick the first matching, whereas we only have a single test to make here.

这篇关于如何写“a == b? X:Y“在Erlang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆