[erlang-questions] learning Erlang: factoring primes
Richard Carlsson
<>
Mon May 7 11:29:41 CEST 2007
ok wrote:
> In Prolog, == means "exact identity" and
> =:= means "numerically equal after evaluation". I cannot imagine why
> Erlang swapped them. I keep on tripping up over this.
Probably so that the shorter symbol represents the term order, which
is probably what the authors considered the more important equality.
(I'm not taking a stand here, just making a guess.)
For two terms X and Y, one of X < Y, X == Y, or X > Y is always true.
If you substitute =:= for ==, none of them is true if e.g. X is 0 and
Y is 0.0. I haven't programmed in Prolog in a long time, so I don't
recall how this is handled there. Isn't there a :< and :> or somesuch?
Here's a mistake I've seen in code that partitions elements:
if X > Y -> ...;
X =:= Y -> ...;
X < Y -> ...
end
this actually throws an if_clause error if X is 0 and Y is 0.0.
In fact, for this kind of code, the explicit test for equality is
redundant and should be skipped in order to avoid problems:
if X > Y -> ...;
X < Y -> ...;
true -> ...
end
now, in the last case, we know that X==Y. (Removing the extra test
also makes the code more efficient.) But this is when you want to
compare elements in the term order (arithmetic equivalence); when
you want exact equality, use =:=.
/Richard
More information about the erlang-questions
mailing list