Tuples vs lists

Vlad Dumitrescu vlad_dumitrescu@REDACTED
Wed Feb 1 13:02:37 CET 2006


Hi and welcome!

> I'm going through the Erlang tutorial,and I don't understand 
> very well the difference between tuples and lists. 

The short answer is:
  Lists are logically meant to represent entities that may vary in length. 
  Entities of fixed length are better represented with tuples.

You could in principle use tuples and lists interchangeably to represent
some data. The difference is in efficiency and storage size. 

A tuple is basically an array, you can do random access to elements and the
elements are stored contiguously in memory. A list has to be traversed to
access its elements, and stores two extra pointers for every element. 

On the other hand, resizing a tuple means that all elements needs to be
copied into a new structure. For a list, adding an element at the head is
very fast and simple.

Hope this helps a little.
Best regards,
Vlad




More information about the erlang-questions mailing list