Professional Documents
Culture Documents
Introduction
A list (also called an array in other programming languages) is a tool that can be used to store
multiple pieces of information at once. It can also be defined as a variable containing multiple other
variables. A list consists of a numbers paired with items. Each item can be retrieved by its paired
number. A list is just a plain old list of items. Slightly more precisely, it is a finite sequence of
elements
In Prolog list is an ordered sequence of components, where components can be variables, constants,
compound terms, numbers or another list.
[1,2,3,4].
[X, Y, james].
[likes(X,Y), cat(fur)].
[] - an empty list
Learning question
Lab objective
1. Concept Map
A list or sequence is an abstract data type that represents a countable number of ordered values,
where the same value may occur more than once.
Lists
Lists are used to handle collections of objects. In Prolog a list can look like:
[a, b, c, d, e]
The brackets are the beginning and the end of the list, and the commas separate the various
elements. Here all the elements are atoms, but a list can contain all sorts of elements, and
different types of element can occur in the same list. The following are examples of valid lists:
[a, A, B, C, D]
[p(A,V), p(a, V)]
[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]
[[A,B], [B,C], quu([a,b,c],[a,b,d,e,f],_), c, m, D]
[mia, robber(honey_bunny), X,
2, mia] []
1. We can specify lists in Prolog by enclosing the elements of the list in square brackets
(that is, the symbols [ and ] ). The elements are separated by commas. For example, the
first list shown above, [mia, vincent, jules, yolanda] , is a list with four elements, namely
mia , vincent , jules , and yolanda . The length of a list is the number of elements it has,
so our first example is a list of length four.
2. From [mia,robber(honey_bunny),X,2,mia] , our second example, we learn that all sorts of
Prolog objects can be elements of a list. The first element of this list is mia , an atom; the
second element is robber(honey_bunny) , a complex term; the third element is X , a
variable; the fourth element is 2 , a number. Moreover, we also learn that the same item
may occur more than once in the same list: for example, the fifth element of this list is
mia , which is same as the first element.
3. The third example shows that there is a special list, the empty list. The empty list (as
its name suggests) is the list that contains no elements. What is the length of the empty
list? Zero, of course (for the length of a list is the number of members it contains, and
the empty list contains nothing).
4. The fourth example teaches us something extremely important: lists can contain
other lists as elements. For example, the second element of
What is the length of the fourth list? The answer is: three. If you thought it was five (or
indeed, anything else) you’re not thinking about lists in the right way. The elements of the
list are the things between the outermost square brackets separated by commas. So this list
contains three elements: the first element is mia , the second element is [vincent, jules] ,
and the third element is [butch, girlfriend(butch)] .
5. The last example mixes all these ideas together. We have here a list which contains the
empty list (in fact, it contains it twice), the complex term dead(z) , two copies of the list
[2, [b, c]] , and the variable Z . Note that the third (and the last) elements are lists which
themselves contain lists (namely [b, c] ).
Example:
Syntax : [H | T]
For example:
(3) In list [likes(john, mary), X , 1, 2] head is likes(john, mary) and tail is [X, 1, 2].
(4) In list [A, [p, n, c], 4] head is A and tail is [[p, n, c],4]
Note : In above examples tails are always a list. [H | T] = [1, 2, 3, 4], H is 1 and T is [2, 3, 4] = [1 |
[2, 3, 4]]
Examples –
X = mia
Y = [vincent,jules,yolanda] yes
ii.
?- [X|Y] = [].
iii.
?- [X,Y | W] = [[], dead(z), [2, [b, c]], [], Z].
X = []
Y = dead(z)
W = [[2,[b,c]],[],_8327]
Z = _8327
yes
X=a
Y=[b,c]
yes
BSCS Artificial Intelligence : LAB TASK BPGCW
on(Item,[DisregardHead|Tail]):- on(Item,Tail).
Let's go through the last example again in more detail. Suppose we pose the query:
The first clause of on requires the first argument of on to match with the list head.
However apples and pears do not match. Thus we must move on to the second clause.
This splits the list up as follows:
DisregardHead = pears
Tail = [tomatoes,apples,grapes]
This now gives us the following goal in the body of our rule:
Again, we see if apples and tomatoes match using our initial facts. Since they don't, we
again use our second clause to strip off the current list head, giving us the new goal:
on(apples,[apples,grapes]). Since apples matches apples our first clause succeeds as
does our query.
NOTE instruction :
compile and execute code that demonstrates use of LIST in prolog , take screen shot of output
regards