Concurrent programming in Erlang

Chapter 3. Programming with Lists

This chapter deals with list processing. Lists are structures used for storing variable numbers of elements. Lists are written beginning with a [ and ending with a ]. The elements of a list are separated by commas. For example, [E1,E2,E3,...] denotes the lists containing the elements E1,E2,E3,...

The notation [E1,E2,E3,...,En|Variable], where n ≥ 1, is used to denote a list whose first elements are E1,E2,E3,...,En and whose remainder is the item denoted by Variable. In the case where n=1, the list has the form [H|T]; this form occurs so frequently that it is conventional to call H the head of the list, and T the tail of the list.

In this chapter we will deal with the processing of proper lists; i.e. lists whose last tails are the empty list [].

It is important to remember that tuples should always be used when dealing with a fixed number of items. Tuples use approximately half the storage of lists and have much faster access. Lists should be used when the problem needs a variable number of items.