Is it a list ?

Joachim Durchholz joachim.durchholz@REDACTED
Thu Jul 10 20:19:25 CEST 2003


Marc Ernst Eddy van Woerkom wrote:
>>  Conjecture: I assume you meant to write [1, 2], which will give a 
>>  well-formed list of the two numbers 1 and 2 :-)
> 
> Or [1|[2]] .. 

Which is the same.

> But why is is_list([1|2]) true?

Because checking the well-formedness of lists would entail O(N) 
overhead, since is_list would have to scan all elements of the list's spine.

> Or asked differently, is there a legitimate use for ill-formed lists?

To a large extent, the question of "legitimate use" is one of tradition 
and experience: things that have proved to have unfortunate consequences 
tend to be called illegitimate by later generations.
In practice, what's legitimate is what the language and environment make 
easy to use.
In this sense, the OTP routines say no. Crashing when being given an 
ill-formed list is a very clear and loud way of saying No :-).
On the other hand, the language says yes - otherwise executing [1|2] 
would crash immediately.
So I don't know whether it's legitimate or not.

Personally, I'd avoid using ill-formed lists.
In addition, I'd recommend that Erlang crashed if the object on the 
right-hand side of | were not a list, so that ill-formed lists cannot be 
constructed.
But that's just my personal tradition and experience :-)

Regards,
Jo




More information about the erlang-questions mailing list