@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.
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.
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).
@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 expressionHowever, 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!
Then your C expression becomes simply
iff(A == B, X, Y)
. However, beware! that just like C, Erlang eagerly evaluates function arguments. IfX
orY
have side effects or are expensive to evaluate, theniff
will not be equivalent to an in-linecase
expression.If you're asking how to write something like
A == B ? X : Y
as anif
expression, it'sYou can also write it as a
case
expression:or
We use macro like this:
Then in your code you write:
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 writea == b
, but that does not explain the general case where we may havea == b ? X : Y
for arbitrary expressionsX
andY
. Also note thata == b
is alwaysfalse
in Erlang, so you could argue that the real thing to do is to replace the whole expression withfalse
.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 thecase
already works.An example:
Suppose we are to return a simple proplist and we have some computation we need to do
But since the
case
construction is an expression, we can just inline it: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 valuea == b
in this case and it can yield one of two outputstrue
orfalse
. In that case thecase
-expression is more direct. Theif
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.Since
a == b ? true : false
maps toa == b
, you can usea == b
in Erlang also.You can use 'if' like that
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).