Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Description
Compute all Things which satisfy the GoalExpresssion and collect them in the
list Bag. If the GoalExpression fails, Bag will be the empty list []. findall treats
all variables in GoalExpression as if they are existentially quantified.
son(X,Y):-
father(Y,X),
male(X).
daughter(X, Y):-
father(Y, X),
female(X).
grandfather(X, Y):-
father(X, Z),
father(Z,Y).
father(afzal, bilal).
father(afzal, zubair).
father(nazir, afzal).
father(afzal, humara).
father(afzal, sumara).
female(sumara).
female(humara).
afzal, afzal].
humara, sumara].
Prolog has three built-in predicates designed to collect together objects resulting
from successful computations:
LAB)
listing(p).
p(1,3,5).
p(2,4,1).
p(3,5,2).
p(4,3,1).
p(5,2,4).
X = 1,
Y = 3,
Bag = [5] ;
X = 2,
Y = 4,
Bag = [1] ;
X = 3,
Y = 5,
Bag = [2] ;
X = 4,
Y = 3,
Bag = [1] ;
X = 5,
Y = 2,
Bag = [4].
2. findall(Z,p(X,Y,Z),Bag).
Bag = [5, 1, 2, 1, 4].
3. bagof(Z,X^Y^p(X,Y,Z),Bag).
Bag = [5, 1, 2, 1, 4].
4. setof(Z,X^Y^p(X,Y,Z),Bag).
Bag = [1, 2, 4, 5].
edge(1,2).
edge(1,4).
edge(1,3).
edge(2,3).
edge(2,5).
edge(3,4).
edge(3,5).
edge(4,5).
path(A,B,Path) :-
travel(A,B,[A],Q),
reverse(Q,Path).
travel(A,B,P,[B|P]) :-
connected(A,B).
travel(A,B,Visited,Path) :-
connected(A,C),
C \== B,
\+member(C,Visited),
travel(C,B,[C|Visited],Path).
?- path(1,5,P).
Eg-1: Find the path between the given two nodes?
P = [1, 2, 5] ;
P = [1, 2, 3, 5] ;
P = [1, 2, 3, 4, 5] ;
P = [1, 4, 5] ;
P = [1, 4, 3, 5] ;
P = [1, 4, 3, 2, 5] ;
P = [1, 3, 5] ;
P = [1, 3, 4, 5] ;
P = [1, 3, 2, 5] ;
false.
%Query
path(1, 7, P).
%Results
P = [1, 2, 4, 3, 7] ;
P = [1, 2, 4, 3, 6, 5, 7] ;
P = [1, 2, 4, 3, 6, 5, 6, 5, 7] ;
P = [1, 2, 4, 3, 6, 5, 6, 5, 6|...] .
Eg-1: Find the path between the given two nodes, where nodes appear in
ascending order?
-------------------------------------------------
%Program
path(X,Y,[X,Y]):- edge(X,Y),X<Y.
path(X,Y,[X|Xs]):-edge(X,W), X<W, path(W,Y,Xs).
-------------------------------------------------
%Query
path(1, 7, P).
%Results
P = [1, 2, 4, 5, 7] ;
P = [1, 4, 5, 7] ;
false.
Eg-3: Find the path between the given two nodes with the given length?
%Results
P = [1, 2, 4, 3, 7] ;
P = [1, 4, 3, 7] ;
false.
CONCLUSION:
Observation
Awarded Mark:
SIGNATURE :
DATE: