怎么写“a==b”? X:Y”换句话说,在 Erlang 中如何编写 C 风格的三元运算符?

发布于 2024-11-09 10:14:26 字数 215 浏览 0 评论 0原文

在 Erlang 中是否有好的方法来编写这样的代码?

A == B ? X : Y

下面是 ruby​​ 风格的代码。这也称为三元运算符

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

A == B ? X : Y

below is ruby-style code. This is also known as a ternary operator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

梦里兽 2024-11-16 10:14:26

解释

三元运算符 _ ? 的原因_ : _ 存在于许多语言中是因为它们有两个语法类:语句和表达式。由于 if-then-else 结构通常属于语句类,因此当您输入表达式时,无法使其发挥作用。因此您添加 _ ? _ : _ 运算符到表达式类。

正如另一篇文章所述,您可以采用 a == b 吗? true : false 并只写 a == b,但这并不能解释我们可能有 a == b 的一般情况? X : Y 用于任意表达式 XY。另请注意,a == b 在 Erlang 中始终为 false,因此您可能会认为真正要做的是将整个表达式替换为 false代码>.

幸运的是,Erlang 与大多数函数式语言一样,只有一种语法类:表达式。因此,您可以使用 X -> case a == b ...; Y-> ... end 在函数中的任何位置,包括其他表达式。换句话说,三元 _ ? _ : _ 运算符在 Erlang 中是多余的,因为 case 已经可以工作了。

举个例子:

假设我们要返回一个简单的 proplist,并且需要进行一些计算,

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

但是由于 case 构造是一个表达式,因此我们可以将其内联:

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

并完成该操作。

为什么我不提倡使用 IF

Erlang 中的 if .. end 结构通常不是你想要的。在这种情况下,您想要仔细检查值 a == b,它可能会产生两个输出 truefalse 之一。在这种情况下,case 表达式更为直接。如果您必须检查多个不同的测试并选择第一个匹配,则更好地使用 if ,而我们在这里只需要进行一个测试。

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.

删除→记忆 2024-11-16 10:14:26

我们使用这样的宏:

-define(IF(Cond,E1,E2), (case (Cond) of true -> (E1); false -> (E2) end)).

然后在您的代码中编写:

io:format("~s~n", [?IF(a==b, "equal", "not equal")]).

We use macro like this:

-define(IF(Cond,E1,E2), (case (Cond) of true -> (E1); false -> (E2) end)).

Then in your code you write:

io:format("~s~n", [?IF(a==b, "equal", "not equal")]).
寒尘 2024-11-16 10:14:26

如果您问如何编写类似 A == B 的内容? X : Y 作为一个 if 表达式,它

if
    A == B ->
        X;
    true ->    % "true" means "else" here
        Y
end

也可以写成一个 case 表达式:

case A == B of
    true ->
        X;
    _Else ->
        Y
end

或者

case A == B of
    true ->
        X;
    false ->
        Y
end

If you're asking how to write something like A == B ? X : Y as an if expression, it's

if
    A == B ->
        X;
    true ->    % "true" means "else" here
        Y
end

You can also write it as a case expression:

case A == B of
    true ->
        X;
    _Else ->
        Y
end

or

case A == B of
    true ->
        X;
    false ->
        Y
end
物价感观 2024-11-16 10:14:26

由于a == b ? true : false 映射到 a == b,您也可以在 Erlang 中使用 a == b

Since a == b ? true : false maps to a == b, you can use a == b in Erlang also.

沫雨熙 2024-11-16 10:14:26

您可以使用“if”,就像

foo(A,B) ->
    [1,
     2,
     (if A == B -> 3; true -> 4 end), % A == B ? 3 : 4
     5,
     6].

特殊的 ?: 形式似乎没有必要。您当然可以使用 true/false 作为返回值,但我认为您的意思是更通用的形式,因为那种形式是无用的( A == B 做同样的工作)。

You can use 'if' like that

foo(A,B) ->
    [1,
     2,
     (if A == B -> 3; true -> 4 end), % A == B ? 3 : 4
     5,
     6].

special ?: form seems to be not necessary. You can of course use true/false as return value but I think you meant more general form, as that one would be useless (A == B does the same job).

暮年 2024-11-16 10:14:26

@Gabe 的答案是最简洁且(据我所知)惯用的。 C 表达式 ((A==B) ? X : Y) 直接映射到 Erlang 表达式

case A == B of
    true -> X;
    false -> Y
end

,但是,这比 C 版本的代码多得多。你可能应该将它包装成一个方便的函数 - 有人应该告诉我这是否已经存在于 Erlang 标准库中!

iff(true, X, Y) -> X;
iff(false, X, Y) -> Y.

那么你的 C 表达式就变成了简单的 iff(A == B, X, Y)但是,请注意!就像 C 一样,Erlang 急切地计算函数参数。如果 XY 有副作用或评估成本昂贵,则 iff 将不等同于内联 case< /代码> 表达式。

@Gabe's answer is the most concise, and (as far as I can tell) idiomatic. The C expression ((A==B) ? X : Y) maps directly to the Erlang expression

case A == B of
    true -> X;
    false -> Y
end

However, this is vastly more code than the C version. You should probably wrap it up into a convenience function — and someone should tell me if this already exists in the Erlang standard libraries!

iff(true, X, Y) -> X;
iff(false, X, Y) -> Y.

Then your C expression becomes simply iff(A == B, X, Y). However, beware! that just like C, Erlang eagerly evaluates function arguments. If X or Y have side effects or are expensive to evaluate, then iff will not be equivalent to an in-line case expression.

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