What are the differences between comparison operators in Erlang?

Asked

Viewed 63 times

6

In Erlang, we have the following comparison operators:

  • =:=
  • =/=
  • ==
  • /=

It is said that the latter two can be used to make comparison between integers and floats, since the first two differentiate one integer of float:

1 =:= 1.0. % false
1 == 1.0. % true

1 =/= 1.0. % true
1 /= 1.0. % false

In addition to this difference, there is some other divergence between the behaviour between the operators =:= and =/= of operators == and /=?

  • From the documentation: == Equal to; /= Not Equal to; =< Less than or Equal to; < Less than; >= Greater than or Equal to; > Greater than; =:= Exactly Equal to; =/= Exactly not Equal to

1 answer

3


Operators == and /= are equity operators, who compare only the value. Operators =:= and =/= are identity operators, who compare type and value. To illustrate, =:= would be equivalent to === in Javascript.

The practical difference was addressed in the question correctly. One can compare an integer with a float, so that 1 == 1.0. % true while 1 =:= 1.0. % false. The technical implication is that equity operators make type conversion, which impairs performance.

It is worth remembering that the comparisons of Pattern matching compare the identity of the patterns, so that:

{X, 3.0} = {2, 3}.
% ** exception error: no match of right hand side value {2,3}

{X, 3} = {2, 3}.
% {2,3}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.