[erlang-questions] What is wrong with this list?

David Hopwood david.hopwood@REDACTED
Tue Sep 4 22:05:27 CEST 2007

Dustin Sallings wrote:
> On Sep 4, 2007, at 10:42 , Lone Wolf wrote:
>> I have been trying this statement:
>> 2> [java | python] = [1, 2, 3, 4, 5, 6].
>> But I got the following error:
>> =ERROR REPORT==== 4-Sep-2007::17:45:07 ===
>> Error in process <0.33.0> with exit value:
>> {{badmatch,[1,2,3,4,5,6]},[{erl_eval,
>> expr,3}]}
>> Why?
>> How to read Erlang errors? they are really obfuscated.
>     I wouldn't say they're obfuscated.  This is actually a quite useful
> error.  It's showing you the value you were trying to pattern match
> against and telling you your match failed.

I'm afraid it *is* obfuscated, compared to stack traces typically produced
by other languages. It also doesn't contain enough information for
debugging: ideally you would want both sides of the failed pattern match.

(In this particular case, that probably wouldn't have helped the OP,
because the problem was a misunderstanding of the syntax of identifiers
and atoms, and "[java | python]" in the error report would probably been
misunderstood in the same way. But it would help in many other cases.)

>     Regarding your statement, it's not clear what you expect it to do. 
> If you capitalize java and python (i.e. make them variables instead of
> atoms), then it'd leave you with the values 1 and [2,3,4,5,6] for Java
> and Python respectively.  Otherwise, it's just going to tell you that
> that list doesn't equal a two element list where the first element is
> the atom ``java'' and the second is the atom ``python.''

Nitpick: it doesn't equal an improper list whose head is the atom ``java''
and whose tail is the (non-list) atom ``python''.

Hmm -- aren't improper lists almost always an error? Why are they allowed?
(Static typing isn't needed to prevent them, and the performance argument
against checking that each tail is a cons cell or nil is weak; it is an
O(1) tag check that can often be optimized out.)

David Hopwood <david.hopwood@REDACTED>

More information about the erlang-questions mailing list