[erlang-questions] What's the difference between tuple and list ?

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Tue Sep 25 13:09:01 CEST 2007

raocheng wrote:
> The Pragmatic Programming Erlang says:
> 1) Suppose you want to group a fixed number of items into a single 
> entity.For this you'd use a tuple.
> 2) We use lists to store variable numbers of things.
> However, we know that variables in Erlang are in essence invariable. So 
> what is the difference between tuple and list ?
> Thanks in advance.

While a certain bound instance of an object will not
change, we often construct new data structures from
old ones. So if I have a variable

Team = [joe, hans, mats]

and want to add an element:

Team1 = [ulf | Persons]

I have constructed a new variable out of the old
one, but I have not changed Team. Still, you could say
that the data object has changed. This becomes clearer
if we think of a process that holds the data as a
function argument

loop(Team) ->
       {add, Member} ->
       {remove, Member} ->
          loop(lists:delete(Member, Team))

In this case, using a list is far better than
using a tuple, since the program doesn't really
know what the number of elements will be.

Ulf W

More information about the erlang-questions mailing list