How to compare two lists?

Lennart Öhman lennart.ohman@REDACTED
Mon May 27 17:25:38 CEST 2002

Hi Dariab!

Comparing lists is a process of first comparing the head of
the list, and if no difference seen, comparing the tails of the
lists. This means that "you" have to traverse the list until
you find the difference, or the entire list to establish that
they are equal.

If you have already traversed the list in order to try to see
if they are equal, you shall not do it again to see if they
are not equal.

My suggestion is:

my_func(L1,L2) ->
        L1 == L2 ->
        true ->

Or perhaps:

my_func(L1,L1) -> equal;
my_func(_,_) -> not_equal.

Of course depending on where you are going to use this, but it is often
considered good practise to not assume anything about what the caller
of the function will use the result for. In other words, sometimes
two lists being equal is the desired result, other times the opposite.
That is why I choose 'equal' and 'not_equal' as return values rather
than {error,...}.

Best Regards,


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.

Lennart Ohman                   phone   : +46-8-587 623 27
Sjoland & Thyselius Telecom AB  cellular: +46-70-552 6735
Sehlstedtsgatan 6               fax     : +46-8-667 8230
SE-115 28 STOCKHOLM, SWEDEN     email   : lennart.ohman@REDACTED

More information about the erlang-questions mailing list