You are on page 1of 6

After mids

Week 11: Introduction to list and use of LIST ,

What is head & tail in LIST.

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.

Syntax : [components] - write yomponents within square brackets.

Examples : different ways to write list

[1,2,3,4].

[nova, james, 2].

[X, Y, james].

[A, [p, n, c], 4].

[likes(X,Y), cat(fur)].

[] - an empty list

How list works? Use of head & tail in prolog.

Learning question

 What is LIST used in PROLOG?


 How LIST helps in developing sequence or order of element?
 What are components used in LIST?
 What are different ways to write list prolog software?

Lab objective

 Understand purpose of lists in prolog.


 Able to use lists and perform basic operation on lists in prolog
 This lab will give overview of list used in prolog to make data order or sequence.
 The student should be able to: Understand the term LIST, COMPONENETS, ORDER,
ELEMENTS
 Students also be able to create order element LIST.
Activity Time: 120 min

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]

We can learn some important things from these examples.


[mia, vincent, jules, yolanda]

[mia, robber(honey_bunny), X,

2, mia] []

[mia, [vincent, jules], [butch,

girlfriend(butch)]] [[], dead(z), [2, [b, c]],

[], Z, [2, [b, c]]]

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

[mia, [vincent, jules], [butch,girlfriend(butch)]

is [vincent,jules] . The third is [butch,girlfriend(butch)] .

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:

Head and Tail of the lists :

head : the 1st element of the list.

tail : all elements of the list except the 1st one

Syntax : [H | T]

For example:

(1) In list [1,2,3,4] head is 1 and tail is [2,3,4]

(2) In list [a] head is a and tail is [].

(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]

(5) In list [[1, 2], a, b ] head is [1, 2] and tail is [a, b]

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]]

Valid: [1 | [2, 3]] [[1,2] | [3, 4]] Invalid: [1, 2 | 3]

Examples –

How do these terms unify ? - Read Unification Instantiation

(1) [A | [b, c]] = [X | Y]


Prolog Responses : ?- [X|Y] = [A | [b, c]] . X = A, Y = [b, c].

?- [A | [b, c]] = [X|Y]. A = X, Y = [b, c].

(2) [B, [p, n, c], 4] = [X | Y]. 


Prolog Response : ?- [B, [p, n, c], 4] = [X | Y]. B = X, Y = [[p, n, c], 4].
(3) [likes(john, mary), X , 1, 2] = [Head | Tail].

Prolog Respsonse : ?- [likes(john, mary), X , 1, 2] = [Head | Tail]. Head = likes(john,


mary),

Tail = [X, 1, 2].

(4) Try it yourself. [[1, 2], a, b ] = [Head | Tail].

(5) Try it yourself. [john] = [Head | Tail].

Sample Prolog Program using List :


Program : /* Facts */ list([p, q, r]).

/* Rules */ what_is([Head | Tail]):- list([Head|Tail]).

Query Prompt : ?- what_is([Head|Tail]). Head = p, Tail = [q, r].

2. Procedure & Tools


Tools
Prolog.

Setting-up Prolog [Expected time = 5 mins]


Open SWIPL, in the File section select Consult option to open existing .pl file or New to
create new prolog file.

Walkthrough Task [Expected time = 45 mins]


To understand list, let’s look at different
examples. i.
?- [X|Y] = [mia, vincent, jules, yolanda].

X = mia
Y = [vincent,jules,yolanda] yes

ii.

?- [X|Y] = [].

no /* Empty lists don’t have head and tails */

iii.
?- [X,Y | W] = [[], dead(z), [2, [b, c]], [], Z].

X = []
Y = dead(z)
W = [[2,[b,c]],[],_8327]
Z = _8327
yes

iv. Consider the following fact.


p([H|T],H,T).
for query ?- p([a,b,c], X, Y).

X=a
Y=[b,c]
yes
BSCS Artificial Intelligence : LAB TASK BPGCW

v. Searching in list example


Let’s say our knowledge base contains on(Item,[Item|Rest]).

on(Item,[DisregardHead|Tail]):- on(Item,Tail).

Let's go through the last example again in more detail. Suppose we pose the query:

?- on(apples, [pears, tomatoes, apples, grapes]).

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:

on(apples, [tomatoes, apples, grapes]).

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.

To Learn More - http://boklm.eu/prolog/page_7.html

NOTE instruction :

compile and execute code that demonstrates use of LIST in prolog , take screen shot of output

submit solved lab: by email me : fa14mcs1592vcomsats.edu.pk

regards

You might also like