Lecture 4: Lists: - Theory
Lecture 4: Lists: - Theory
• Theory
– Introduce lists, an important recursive data
structure often used in Prolog programming
– Define the member/2 predicate, a fundamental
Prolog tool for manipulating lists
– Illustrate the idea of recursing down lists
Lecture 4: Lists
• Exercises
– Exercises of LPN chapter 4
– Practical work
Lists
elements it has
• All sorts of Prolog terms can be
elements of a list
• There is a special list:
the empty list [ ]
Head and Tail
– 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
Head and Tail example 1
Head:
Tail:
Head and Tail example 1
Head: mia
Tail:
Head and Tail example 1
Head: mia
Tail: [vincent, jules, yolanda]
Head and Tail example 2
Head:
Tail:
Head and Tail example 2
Head: [ ]
Tail:
Head and Tail example 2
Head: [ ]
Tail: [dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head and Tail example 3
• [dead(z)]
Head:
Tail:
Head and Tail example 3
• [dead(z)]
Head: dead(z)
Tail:
Head and Tail example 3
• [dead(z)]
Head: dead(z)
Tail: [ ]
Head and tail of empty list
Head = mia
Tail = [vincent,jules,yolanda]
yes
?-
The built-in operator |
X = mia
Y = [vincent,jules,yolanda]
yes
?-
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?-
no
?- [X|Y] = [ ].
The built-in operator |
The built-in operator |
X=[]
Y = dead(z)
Z = _4543
Tail = [[2, [b,c]], [ ], Z, [2, [b,c]]]
yes
?-
Anonymous variable
?-
Anonymous variables
?-
member/2
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?-
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(yolanda,[yolanda,trudy,vincent,jules]).
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(yolanda,[yolanda,trudy,vincent,jules]).
yes
?-
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(vincent,[yolanda,trudy,vincent,jules]).
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(vincent,[yolanda,trudy,vincent,jules]).
yes
?-
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(zed,[yolanda,trudy,vincent,jules]).
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(zed,[yolanda,trudy,vincent,jules]).
no
?-
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(X,[yolanda,trudy,vincent,jules]).
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(X,[yolanda,trudy,vincent,jules]).
X = yolanda
member/2
member(X,[X|T]).
member(X,[H|T]):- member(X,T).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- member(X,[yolanda,trudy,vincent,jules]).
X = yolanda;
X = trudy;
X = vincent;
X = jules;
no
Rewriting member/2
member(X,[X|_]).
member(X,[_|T]):- member(X,T).
Recursing down lists
a2b([],[]).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b([a,a,a],[b,b,b]).
Testing a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b([a,a,a],[b,b,b]).
yes
?-
Testing a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b([a,a,a,a],[b,b,b]).
Testing a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b([a,a,a,a],[b,b,b]).
no
?-
Testing a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b([a,t,a,a],[b,b,b,c]).
Testing a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b([a,t,a,a],[b,b,b,c]).
no
?-
Further investigating a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b([a,a,a,a,a], X).
Further investigating a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b([a,a,a,a,a], X).
X = [b,b,b,b,b]
yes
?-
Further investigating a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b(X,[b,b,b,b,b,b,b]).
Further investigating a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- a2b(X,[b,b,b,b,b,b,b]).
X = [a,a,a,a,a,a,a]
yes
?-
Summary of this lecture