3.11 List
A list is a compound data type with a variable number of terms.
- [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> L1 = [a,2,{c,4}].
- [a,2,{c,4}]
- 2> [H|T] = L1.
- [a,2,{c,4}]
- 3> H.
- a
- 4> T.
- [2,{c,4}]
- 5> L2 = [d|T].
- [d,2,{c,4}]
- 6> length(L1).
- 3
- 7> length([]).
- 0
A collection of list processing functions can be found in the lists manual page in STDLIB.