List Manipulation in Prolog
List is one of the most important data structure in Prolog. Lists are contained in square brackets with the element being separated by commas. Here’s is an example.
[Nepal, India, Bhutan, Sri Lanka, Maldives, Pakistan, Bangladesh]
This is the list of seven atoms Nepal, India, Bhutan, Sri Lanka, Maldives, Pakistan, Bangladesh. Element of lists could be any valid Prolog terms, i.e. atoms, numbers, variables or compound terms. This includes other lists too. The empty list is written as . The following is another example of list.
[Mango, X, , son (Dasarath, X), [x, y, z], func(5)]
Head and Tail in List
The ﬁrst element of a list is called its head and the remaining list is called the tail. An empty list doesn’t have a head. A list just containing a single element has a head (namely that particular single element) and its tail is the empty list. In above example of lists of countries in SAARC, Nepal is head and remaining countries constitute tail and this is represented as
[Nepal | [India, Bhutan, Sri Lanka, Maldives, Pakistan, Bangladesh]]
A list containing exactly one element has one head and empty tail. We will see some operations on List in Next Tutorial.