Is it a list ?

Joachim Durchholz joachim.durchholz@REDACTED
Thu Jul 10 14:47:31 CEST 2003


Zoltan Peter Toth wrote:
> Hi !
> 
> I've made a little test in the Erlang machine:
> 
> 1> X = [1|2].
> [1|2]

A well-formed list has the structure
   +---+---+  +---+---+         +---+---+
   | x | x--->| x | x---> ...-->| x |NUL|
   +-|-+---+  +-|-+---+         +-|-+---+
     |          |                 |
     V          V                 V
    data       data              data

This kind of list is written as
   [data, data, ..., data]
in Erlang.

Your list is a very short specimen of a not well-formed list, which has 
the form
   +---+---+  +---+---+         +---+---+
   | x | x--->| x | x---> ...-->| x | x ---> data
   +-|-+---+  +-|-+---+         +-|-+---+
     |          |                 |
     V          V                 V
    data       data              data
The final link of the "list spine" at the right side is what makes the 
list "not well-formed". (This is the only way to make a list not 
well-formed. Other data structures usually have more ways to be not 
well-formed, but lists are so simple that there's just a single way.)

This kind of list is written as
   [data, data, ... | data]
in Erlang (note the final | symbol).


If a list is constructed using |, the object to the right of the | must 
be a well-formed list if the  total result should be a well-formed list. 
(Can you see why? (Actually, I'm just too lazy to spell it out *gg*))


Conjecture: I assume you meant to write [1, 2], which will give a 
well-formed list of the two numbers 1 and 2 :-)

> .. so what is the structure

   +---+---+
   | x | x---> 2
   +-|-+---+
     |
     V
     1

 > and what are the elements

It's only element is 1.
If one accepts that a list tail may be a non-list for a not well-formed 
list, then 2 is the tail of the list. (Most people wouldn't accept this 
though.)

 > and the length of this weird list ?

1. The lenght of a list is the length of its spine.
By another definition, the lenght is undefined since the list is not 
well-formed.

HTH
Jo




More information about the erlang-questions mailing list