4. HOW TO MAKE A LIST IN PROLOG
pam tom
bob liz
ann pat
jim
5. HOW TO MAKE A LIST IN PROLOG
parent(pam, bob).
pam tom
parent(tom, bob).
parent(tom, liz).
parent(bob, pat).
liz parent(pat, jim).
bob
female(pam).
female(liz).
female(pat).
ann pat male(tom).
male(bob).
male(jim).
jim
6. HOW TO MAKE A LIST IN PROLOG
pam tom
bob liz
ann pat
jim
7. HOW FINDALL WORKS
findall(X,predecessor(pam,X),Preds).
Output list
The query variable
8. EXAMPLE1
The following conversation with Prolog:
?- Hobbies1 = [tennis, music],
Hobbies2 = [skiing, food],
L = [ann, Hobbies1, tom, Hobbies2].
Hobbies1 = [tennis, music]
Hobbies2 = [skiing, food]
L = [ann, [tennis, music], tom, [skiing, food] ]
8
9. EXAMPLE2
Treat the whole tail as a single object:
L = [a, b, c]
Tail = [b, c]
L = .(a, Tail)
L = [a | Tail]
[a, b, c] = [a | [b, c] ] = [a, b | [c] ] = [a, b, c | [ ] ]
10. EXAMPLE2
Treat the whole tail as a single object:
L = [a, b, c]
Tail = [b, c]
L = .(a, Tail)
L = [a | Tail]
[a, b, c] = [a | [b, c] ] = [a, b | [c] ] = [a, b, c | [ ] ]
11. MEMBERSHIP
X is a member of L either:
1) X is the head of L, or
2) X is a member of the tail of L.
member(X, L)
Can be written in two clauses:
member(X, [X | Tail] ). %simple fact
member(X, [Head | Tail] ) :-
member(X, Tail). %rule
Example:
member(b, [a,b,c] ). %true
member(b, [a,[b,c]] ). %false, the tail is [b,c]
member([b,c],[a,[b,c]] ). %true
13. MEMBERSHIP
Parents = [pam, tom, tom, bob, pat].
Is pam a member of HEAD of Parents????
member(pam,[pam,Parents]).
14. 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
15. HEAD AND TAIL EXAMPLE 1
[mia, vincent, jules, yolanda]
Head:
Tail:
16. HEAD AND TAIL EXAMPLE 1
[mia, vincent, jules, yolanda]
Head: mia
Tail:
17. HEAD AND TAIL EXAMPLE 1
[mia, vincent, jules, yolanda]
Head: mia
Tail: [vincent, jules, yolanda]
18. HEAD AND TAIL EXAMPLE 2
[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head:
Tail:
19. HEAD AND TAIL EXAMPLE 2
[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]
Head: [ ]
Tail:
20. 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]]]
28. 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
?-
29. ANONYMOUS VARIABLES
?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda].
X2 = vincent
X4 = jody
yes
?-
There is a simpler way of obtaining only
the information we want:
The underscore is the anonymous
variable
30. THE ANONYMOUS VARIABLE
Is used when you need to use a variable, but
you are not interested in what Prolog
instantiates it to
Each occurrence of the anonymous variable
is independent, i.e. can be bound to
something different
31. MEMBER
One of the most basic things we would like to
know is whether something is an element of
a list or not
So let`s write a predicate that when given a
term X and a list L, tells us whether or not X
belongs to L
This predicate is usually called member/2
40. MEMBER/2
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
41. CONCATENATION OF LISTS
For concatenation we first define our rule.
Step 1: if the first list is empty
conc([],L,L).
Step2: if first list is not empty
conc(L1,L2,L3)=> conc([X|L1],L2,[X|L3]):-
conc(L1,L2,L3).
44. LENGTH OF A LIST IN PROLOG
len([],0). %% not a Prolog native predicate!
len([_|L],N):-
len(L,X),
N is X + 1.
?- len([a,b,c,d,e,[a,x],t],X).
X=7
yes
?-