List is a simple data structure that can contain any number of items. Individual items of a list are placed within square brackets ([]). For example, a list of students, we will write, [‘Manya’, ‘Amyra’,’Raman’,’John’,’Zeenat’]. We can also define an empty list by writing, a Prolog atom, [].

A list with element(s) have two important things,

  • Head which is the first item of the list
  • Tail that is the remaining part of the list

In the list, [‘Manya’,’Amyra’,’Raman’,’John’,’Zeenat’], ‘Manya’ is the head of the list. Tail is another list consisting of [‘Amyra’,’Raman’,’John’,’Zeenat’].

Note that if we have a list, L = [a, b, c]. Then, Tail = [b, c]

This can be also written as L = [ a | Tail].

Here, the vertical bar (|) separates the head and tail parts.

List can also be represented as,

  • [a, b, c] = [a, | [b, c] ]
  • [a, b, c] = [a, b | [c] ]
  • [a, b, c] = [a, b, c | [ ] ]

Basic Operations on Lists

Table lists some operations that can be performed on lists in Prolog.

images

Membership Operation

To check membership, we define a predicate, list_member(X,L) that will consider two things. If X is a member of a list, then either X is head of the list or is a member of the tail of the list.

To implement this predicate, create a new file in text editor, write the following lines of code in it and save it as kb5.pl.

images
images

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *