Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Lists



In this chapter, we will discuss one of the important concepts in Prolog, The Lists. It is a data structure that can be used in different cases for non-numeric programming. Lists are used to store the atoms as a collection.

In the subsequent sections, we will discuss the following topics −

  • Representation of lists in Prolog

  • Basic operations on prolog such as Insert, delete, update, append.

  • Repositioning operators such as permutation, combination, etc.

  • Set operations like set union, set intersection, etc.

Representation of Lists

The list is a simple data structure that is widely used in non-numeric programming. List consists of any number of items, for example, red, green, blue, white, dark. It will be represented as, [red, green, blue, white, dark]. The list of elements will be enclosed with square brackets.

A list can be either empty or non-empty. In the first case, the list is simply written as a Prolog atom, []. In the second case, the list consists of two things as given below −

  • The first item, called the head of the list;

  • The remaining part of the list, called the tail.

Suppose we have a list like: [red, green, blue, white, dark]. Here the head is red and tail is [green, blue, white, dark]. So the tail is another list.

Now, let us consider we have a list, L = [a, b, c]. If we write Tail = [b, c] then we can also write the list L as L = [ a | Tail]. Here the vertical bar (|) separates the head and tail parts.

So the following list representations are also valid −

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

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

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

For these properties we can define the list as −

A data structure that is either empty or consists of two parts − a head and a tail. The tail itself has to be a list.

Creating a List of three atoms.

Program (list_basics.pl)

:- initialization(main).
main :- write([a, b, c]).

Output

| ?- consult('D:/TP Prolog/Sample Codes/list_basics.pl').
compiling D:/TP Prolog/Sample Codes/list_basics.pl for byte code...
D:/TP Prolog/Sample Codes/list_basics.pl compiled, 1 lines read - 419 bytes written, 3 ms
[a,b,c]

yes
| ?- 

Creating a List of two atoms and a tail of one atom.

Program (list_basics.pl)

:- initialization(main).
main :- write([a, b | [c] ]).

Output

| ?- consult('D:/TP Prolog/Sample Codes/list_basics.pl').
compiling D:/TP Prolog/Sample Codes/list_basics.pl for byte code...
D:/TP Prolog/Sample Codes/list_basics.pl compiled, 1 lines read - 419 bytes written, 3 ms
[a,b,c]

yes
| ?- 

Creating a List of three atoms and an empty tail

Program (list_basics.pl)

:- initialization(main).
main :- write([a, b, c | [ ] ]).

Output

| ?- consult('D:/TP Prolog/Sample Codes/list_basics.pl').
compiling D:/TP Prolog/Sample Codes/list_basics.pl for byte code...
D:/TP Prolog/Sample Codes/list_basics.pl compiled, 1 lines read - 419 bytes written, 3 ms
[a,b,c]

yes
| ?- 
Advertisements