How to compare two lists?
Raimo Niskanen
raimo@REDACTED
Tue May 28 09:08:20 CEST 2002
According to the Erlang Book (Concurrent Programming in Erlang),
<http://erlang.ericsson.se/publications/erlang-book-part1.pdf>, chapter
2.5.6, comparision is done as follows:
<BLOCKQUOTE>
The comparison operators work as follows: firstly, both sides of the
operator are
evaluated where possible (i.e. in the case when they are arithmetic
expressions, or
contain guard function BIFs); then the comparison operator is performed.
For the purposes of comparison the following ordering is defined:
number < atom < reference < port < pid < tuple < list
Tuples are ordered first by their size then by their elements. Lists
are ordered
by comparing heads, then tails.
</BLOCKQUOTE>
According to that last paragraph, lists are indeed traversed by the '=='
operator, so you should be able to write:
my_func(List1, List2) ->
if
List1 == List2 -> true;
true -> false;
end.
or even without the '==' operator:
my_func(L, L) -> true;
my_func(_, _) -> false.
or why do you need my_func/2 , it just does:
List1 == List2
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
Dariab Wahed wrote:
>
> Hi!
> Can you help me,
>
> How to compare two lists?
> eg.
>
> my_func(List1,List2)
> if
> List1 == List2 -> ok;
> List1/= List2 -> {error,"error!!!!"};
> end.
More information about the erlang-questions
mailing list