Professional Documents
Culture Documents
Brahim Hnich
Lists in Prolog
One of the most useful data structures in Prolog are lists. The
objective of this lecture is to show you how lists are represented in
Prolog and to introduce you to the basic principles of working with
lists.
Internal Representation
That means, this is just a new notation. Internally, lists are just
compound terms with the functor . (dot) and the special atom [] as
an argument on the innermost level.
We can verify this also in Prolog:
?- X = .(a, .(b, .(c, []))).
X = [a, b, c]
Yes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
3
List Manipulation
If a bar | is put just before the last term in a list, it means that this
last term denotes a sub-list.
Inserting the elements before the bar at the beginning of the sub-list
yields the entire list.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
4
List Manipulation
Examples
X = b
Yes
Make sure the first element is a 1 and get the sub-list after the
second element:
?- MyList = [1, 2, 3, 4, 5], MyList = [1, | Rest].
MyList = [1, 2, 3, 4, 5]
Rest = [3, 4, 5]
Yes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
5
List Manipulation
Another example:
?- [elephant] = [X | Y].
X = elephant
Y = []
Yes
Note: The tail of a list is always a list itself. The head of a list is an
element of that list. The head could also be a list itself.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
7
List Manipulation
Appending Lists
Yes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
8
List Manipulation
Solution
In every recursion step we take off the head and use the same
predicate again, with the (shorter) tail, until we reach the base case.
concat lists([], List, List).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
9
List Manipulation
Do More
List = [4, 5]
Yes
N = 4
Yes
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
11
List Manipulation
Membership
Yes
X = dog ;
X = tiger ;
X = elephant ;
No
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
12
List Manipulation
Membership
member(X,[X| ]).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
13
List Manipulation
List notation:
normal: [Elem1, Elem2, Elem3] (empty list: [])
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
14
Applications
Example*
Displaying the Elements of a List
member(Element, List),
write(Element),
nl,
fail.
Example*
Displaying the Elements of a List
Example*
Finding the last element in a list
last(X,[X]).
last(X,[ |T]) :-
last(X,T).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
17
Applications
Example*
Finding the last but one element in a list
lastbutone(X,[X, ]).
lastbutone(X,[ ,Y|T]) :-
lastbutone(X,[Y|T]).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
18
Applications
Example*
Duplicate each element of a list
dupli([],[]).
dupli([X|Xs],[X,X|Ys]) :-
dupli(Xs,Ys).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
19
Applications
Example*
Prefix
P = [] ;
P = [a] ;
P = [a,b] ;
P = [a,b,c] ;
P= [a,b,c,d] ;
No
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
20
Applications
Example*
Prefix
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
21
Applications
Example*
Suffix
S = [] ;
S = [d] ;
S = [c,d] ;
S = [b,c,d] ;
S = [a,b,c,d] ;
No
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
22
Applications
Example*
Suffix
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
23
Applications
Example*
Finding Sublists
Example*
Finding Sublists
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
25
Applications
Example**
Flatten a nested list structure
my flatten(X,[X]) :- \+ is list(X).
my flatten([],[]).
my flatten([X|Xs],Zs) :-
my flatten(X,Y),
my flatten(Xs,Ys),
append(Y,Ys,Zs).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
26
Applications
Example*
Reversing a list
?-myreverse([a,[b,c],d],R).
R = [d,[b,c],a]
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
27
Applications
Example*
Reversing a List
myreverse([],[]).
myreverse([H|T],R) :-
myreverse(T,RT),
append(RT,[H],R).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
28
Applications
Example*
Find out whether a list is a palindrome
palindrome(L) :-
reverse(L,L).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
29
Applications
Example**
Reversing a list w/o using append/3
rev([], R, R).
rev([H|T], L, R) :-
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
30
Applications
Example**
Eliminate consecutive duplicates of list elements
compress([],[]).
compress([X],[X]).
compress([X,X|Xs],Zs) :-
compress([X|Xs],Zs).
compress([X,Y|Ys],[X|Zs]) :-
X \= Y, compress([Y|Ys],Zs).
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
31
Applications
Example**
Pack consecutive duplicates of list elements into sublists
pack([],[]).
pack([X|Xs],[Z|Zs]) :- transfer(X,Xs,Ys,Z), pack(Ys,Zs).