How to write "a == b? X: Y" in Erlang?

Is there a good way to write such code in Erlang?

A == B ? X : Y 

below is the ruby-style code

+7
source share
6 answers

Description

The reason the ternary operator _ ? _ : _ _ ? _ : _ exists in many languages, due to the fact that they have two syntactic classes: expressions and expressions. Since if-then-else constructs usually belong to the statement class, there is no way to get this when you enter an expression. Therefore, you add the _ ? _ : _ operator _ ? _ : _ _ ? _ : _ to the expression class.

As another message state, can you take a == b ? true : false a == b ? true : false and just write a == b , but that does not explain the general case when we can have a == b ? X : Y a == b ? X : Y for arbitrary expressions X and Y Also note that a == b always false in Erlang, so you can argue that the real task is to replace the entire expression with false .

Fortunately, Erlang, like most functional languages, has only one syntax class, expressions. Therefore, you can use case a == b of X -> ...; Y -> ... end case a == b of X -> ...; Y -> ... end anywhere in the function, including other expressions. In other words, the ternary operator _ ? _ : _ _ ? _ : _ redundant in Erlang since case already running.

Example:

Suppose we need to return a simple leaflet and we need to figure out what 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 construct is an expression, we can simply insert it:

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

and run with the thing.

Why I'm not a fan of using IF

The if .. end construct in Erlang is usually not what you want. In this case, you want to examine the value a == b , and it can give one of two outputs true or false . In this case, the case expression is more direct. if better to use if you need to test several different tests and select the first match, whereas we only have one test to do here.

+34
source

If you ask how to write something like A == B ? X : Y A == B ? X : Y as an if , this

 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 
+5
source

We use the macro as follows:

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

Then in the code you write:

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

So how is a == b ? true : false a == b ? true : false displayed in a == b , you can also use a == b in Erlang.

+4
source

You can use 'if' like this

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

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

+1
source

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

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

However, this is a lot more code than version C. You should probably include it in the convenience function - and someone should tell me if this already exists in the standard Erlang libraries!

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

Then your C expression will just become iff(A == B, X, Y) . However, be careful! 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 the expression in the case string.

0
source

All Articles