lab3

Chapter Three: Lists, Operators,
Arithmetic
1
Important things about lists



List elements are enclosed in square brackets
The length of a list is the number of elements it has
There is a special list:
the empty list [ ]
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail

A non-empty list can be thought of as consisting of two
parts




The head
The tail
The head is the first item in the list
The tail is everything else


The tail is the list that remains when we take the first element
away
The tail of a list is always a list
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 1

[mia, vincent, jules, yolanda]
Head:
Tail:
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 1

[mia, vincent, jules, yolanda]
Head:
Tail:
mia
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 1

[mia, vincent, jules, yolanda]
Head:
Tail:
mia
[vincent, jules, yolanda]
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 2

[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head:
Tail:
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 2

[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head: [ ]
Tail:
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 2

[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head: [ ]
Tail: [dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 3

[dead(z)]
Head:
Tail:
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 3

[dead(z)]
Head: dead(z)
Tail:
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Head and Tail example 3

[dead(z)]
Head: dead(z)
Tail: [ ]
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
The built-in operator |


Prolog has a special built-in operator | which can be used
to decompose a list into its head and tail
The | operator is a key tool for writing Prolog list
manipulation predicates
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
The built-in operator |
?- [Head|Tail] = [mia, vincent, jules, yolanda].
Head = mia
Tail = [vincent,jules,yolanda]
yes
?-
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
The built-in operator |
?- [X|Y] = [mia, vincent, jules, yolanda].
X = mia
Y = [vincent,jules,yolanda]
yes
?-
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
The built-in operator |
?- [X,Y|Tail] = [[ ], dead(z), [2, [b,c]], [], Z, [2, [b,c]]] .
X=[]
Y = dead(z)
Tail = [[2, [b,c]], [ ], Z, [2, [b,c]]]
yes
?-
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Anonymous variable

Suppose we are interested in the second and fourth
element of a list
?- [X1,X2,X3,X4|Tail] = [mia, vincent, marsellus, jody, yolanda].
X1 = mia
X2 = vincent
X3 = marsellus
X4 = jody
Tail = [yolanda]
yes
?© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
Anonymous variables

There is a simpler way of obtaining only the
information we want:
?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda].
X2 = vincent
X4 = jody
yes
?-
The underscore is the anonymous
variable
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
3.2 Some operations on lists
1.
2.
3.
4.
19
Membership
Concatenation
Deleting an item
Permutations
3.2 Some operations on lists
1. Member
member(X,L) where X is an object and L is a list
member(X,L) is true if X occurs in L
e.g. member(b, [a,b,c])
member(b, [a,[b,c]])
member([b,c], [a,[b,c]])
Definition of member:
X is a member of L if either:
(1) X is the head of L, or
(2) X is a member of the tail of L.
20



member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?-
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?- member(yolanda,[yolanda,trudy,vincent,jules]).
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?- member(yolanda,[yolanda,trudy,vincent,jules]).
yes
?-
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?- member(vincent,[yolanda,trudy,vincent,jules]).
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?- member(vincent,[yolanda,trudy,vincent,jules]).
yes
?-
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?- member(zed,[yolanda,trudy,vincent,jules]).
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?- member(zed,[yolanda,trudy,vincent,jules]).
no
?-
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?- member(X,[yolanda,trudy,vincent,jules]).
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
?- member(X,[yolanda,trudy,vincent,jules]).
X = yolanda;
X = trudy;
X = vincent;
X = jules;
no
© Patrick Blackburn, Johan
Bos & Kristina Striegnitz
3.2 Some operations on lists
2. Concatenation
conc(L1,L2,L3) here L1 and L2 are two lists and L3 is
their concatenation.

conc([a,b],[c,d],[a,b,c,d])

conc([a,b],[c,d],[a,b,a,c,d])
Definition of conc:
(1) If the first argument is empty, then the second and third
arguments must be the same list conc([],L,L).
the result of concatenation of L1 and L2 is the list L3
where L3 is the concatenation of L1 and L2
30
3.2 Some operations on lists
2. Concatenation
Examples
?- conc([a,b,c],[1,2,3],L)
L=[a,b,c,1,2,3]
?-conc([a,[b,c],d],[a,[],b],L)
L=[a,[b,c],d,a,[],b]
31
3.2 Some operations on lists
2. Concatenation
Examples
?- conc(L1,L2,[a,b,c])
L1=[]
L2=[a,b,c];
L1=[a]
L2=[b,c];
L1=[a,b]
L2=[c];
L1=[a,b,c]
L2=[];
no
32
(decompose)
3.2 Some operations on lists
2. Concatenation
Examples
How can we find the months that precede and the
months that follow a given month?
?-conc(Before, [may|After],
[jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec]).
Before = [jan, feb, mar, apr]
After = [jun, jul, aug, sep, oct, nov, dec]
33
3.2 Some operations on lists
4. Deleting an item
Deleting item X from a list L can be programmed as a relation:
del(X,L,L1)
where L1 is equal to the list L with the item X removed.
Definition of del:
It can be defined similarly to the membership relation, again we
have 2 cases:
1.
If X is the head.
2.
If X is in the tail.
del(X,[X|Tail],Tail).
del(X,[Y|Tail],[Y,Tail1]):- del(X,Tail,Tail1).
34
3.2 Some operations on lists
4. Deleting an item
Example:
?- del(a,[a,b,a,a],L).
L = [b,a,a];
L = [a,b,a];
L = [a,b,a];
no
35
3.2 Some operations on lists
4. Deleting an item
Example:
If we want to insert ‘a’ in the list [1,2,3] then we can do that by
asking the question:
What is L such that after deleting ‘a’ from L we obtain [1,2,3]?
?- del(a,L,[1,2,3]).
L = [a,1,2,3];
L = [1,a,2,3];
L = [1,2,a,3];
L = [1,2, 3,a];
no
36
3.2 Some operations on lists
5. Permutations
Permutation relation has two arguments(lists), one is a
permutation of the other.
e.g.
?- permutation([a,b,c],P).
P=[a,b,c];
P=[a,c,b];
P=[b,a,c];
…
37
3.2 Some operations on lists
5. Permutations
1. If the first list is empty, then the second must be
also empty.
permutation([],[]).
38