Information Technology Reference
In-Depth Information
Figure 7.2.
Another trace of a list predicate
?- join([a,b,c,d],[e,f],R).
Call: (3) join([a,b,c,d],[e,f],_G306)
Call: (4) join([b,c,d],[e,f],_G378)
Call: (5) join([c,d],[e,f],_G381)
Call: (6) join([d],[e,f],_G384)
Call: (7) join([],[e,f],_G387)
% Now the first clause is used.
Exit: (7) join([],[e,f],[e,f])
% It succeeds immediately.
Exit: (6) join([d],[e,f],[d,e,f])
Exit: (5) join([c,d],[e,f],[c,d,e,f])
Exit: (4) join([b,c,d],[e,f],[b,c,d,e,f])
Exit: (3) join([a,b,c,d],[e,f],[a,b,c,d,e,f])
R = [a,b,c,d,e,f]
Yes
3.
The new query is then join(T,L,Z) , that is, join([b,c,d],[e,f],Z) .
4.
This subquery does not unify with the head of the first clause, but it does unify
with the head of the second clause.
5.
Eventually, this subquery succeeds with Z=[b,c,d,e,f] .
6.
Then the top-level query succeeds with R=[a|Z] , that is, R=[a,b,c,d,e,f] .
7.3 Using the member and append predicates
From now on, the built-in Prolog predicates member and append (which behave like
elem and join ) are used in the examples. One very useful application of member is to
generate the elements of a list:
?- member(X,[a,b,c]).
X=a ;
X=b ;
X=c ;
No
This means that queries (or bodies of clauses) can be written that go through the
elements of a list looking for one that satisfies some condition:
... member(X,L), p(X), ...
 
 
Search WWH ::




Custom Search