Is it a list ?

Luke Gorrie luke@REDACTED
Thu Jul 10 20:33:50 CEST 2003


Marc Ernst Eddy van Woerkom <Marc.Vanwoerkom@REDACTED> writes:

> >   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]] .. 
> 
> But why is is_list([1|2]) true?

Probably because a proper test would mean traversing all the way to
the end, which could be expensive. Instead, it's common in Lispy
languages to define a 'list' as "either a cons or null/[]", and to
treat the "properly" null-terminated list as a different, informal
type.

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

[1|2] takes up less memory (two numbers, one cons cell) than [1,2]
(really [1|[2|[]]] - two numbers, two cons cells, and [].) {1,2} also
uses more space than [1|2] since it needs to record the length.

The 'digraph' module of stdlib represents edges as ['$e'|UniqueNumber]
for this memory efficiency.

These "improper" lists are also an excellent way to break other
languages' implementations of the Erlang external term format. IIRC,
JInterface wants to map Erlang lists onto Java's array-based
List/Vector types, but you can't map an improper list onto those. That
can cause troubles when you accidentally pass improper lists around
(e.g. 'catch' can return them.)

Cheers,
Luke




More information about the erlang-questions mailing list