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),
<>, chapter
2.5.6, comparision is done as follows:


The comparison operators work as follows: firstly, 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 defined:

	number < atom < reference < port < pid < tuple < list

Tuples are ordered first by their size then by their elements. Lists
are ordered
by comparing heads, then tails.


According to that last paragraph, lists are indeed traversed by the '=='
operator, so you should be able to write:
	my_func(List1, List2) ->
	        List1 == List2 -> true;
		true -> false;

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