Page 85 - DCAP310_INTRODUCTION_TO_ARTIFICIAL_INTELLIGENCE_AND_EXPERT_SYSTEMS
P. 85
Unit 5: Artificial Intelligence Programming Language
Notes
Example 2: Consider the following fact.
p([H|T], H, T).
Lets see what happens when we ask some simple queries.
?- p([a,b,c], X, Y).
X=a
Y=[b,c]
yes
?- p([a], X, Y).
X=a
Y=[ ]
yes
?- p([], X, Y).
no
5.6.2 Prolog Sequences
The kind of sequences most used in Prolog is “comma” sequences. There is no empty sequence
(unlike for lists). The shortest sequence has one element. Longer sequences have elements
separated by commas “,”. An appropriate declaration for the comma operator would be
:- op(1000,xfy,’,’).
meaning that comma is right-associative with precedence 1000. (The comma operator is actually
built-in.) Here is some Prolog behavior.
?- (H,T) = (1,2,3,4).
H = 1
T = 2,3,4
?- (a) = a.
Yes
?- (H,T) = (a).
No
?- (A,B,C) = (1,2,3,4,5).
A = 1
B = 2
C = 3,4,5
Prolog clauses use comma sequences.
?- assert((a(X):- b(X),c(X),d(X))). %% Note parens around clause
X = G1056
?- clause(a(X),Body), Body=(First,Next).
First = b(G1176)
Next = c(G1176), d(G1176)
Body = b(G1176), c(G1176), d(G1176)
X = G1176
Processing sequences is similar to processing lists, except that the base case for sequences is a
unit sequence (one element), whereas for lists the base case is for the empty list.
LOVELY PROFESSIONAL UNIVERSITY 79