3.11 List

A list is a compound data type with a variable number of terms.

  1. [Term1,...,TermN]

Each term Term in the list is called an element. The number of elements is said to be the length of the list.

Formally, a list is either the empty list [] or consists of a head (first element) and a tail (remainder of the list). The tail is also a list. The latter can be expressed as [H|T]. The notation [Term1,…,TermN] above is equivalent with the list [Term1|[…|[TermN|[]]]].

Example:

[] is a list, thus [c|[]] is a list, thus [b|[c|[]]] is a list, thus [a|[b|[c|[]]]] is a list, or in short [a,b,c]

A list where the tail is a list is sometimes called a proper list. It is allowed to have a list where the tail is not a list, for example, [a|b]. However, this type of list is of little practical use.

Examples:

  1. 1> L1 = [a,2,{c,4}].
  2. [a,2,{c,4}]
  3. 2> [H|T] = L1.
  4. [a,2,{c,4}]
  5. 3> H.
  6. a
  7. 4> T.
  8. [2,{c,4}]
  9. 5> L2 = [d|T].
  10. [d,2,{c,4}]
  11. 6> length(L1).
  12. 3
  13. 7> length([]).
  14. 0

A collection of list processing functions can be found in the lists manual page in STDLIB.