我有两个问题困扰了我好几个小时。connected/2
应该判断两个人是否有联系;distance/3
应该衡量亲属关系。但:
- 对于查询,我不断得到
true
sconnected(x,y)
; N
而且我的distance(x,y,N)
查询越来越多。有什么建议么?
以下是我的事实:
male(ted).
male(barney).
male(ranjit).
male(marshall).
male(tony).
male(swarley).
male(steve).
male(chuck).
male(john).
male(devon).
male(morgan).
female(robin).
female(lily).
female(wendy).
female(stellar).
female(abby).
female(victoria).
female(carina).
female(sarah).
female(ellie).
married(ted, robin).
married(marshall, lily).
married(ranjit, wendy).
married(stellar, tony).
married(steve, carina).
married(sarah, chuck).
married(ellie, devon).
father(ted, barney).
father(ted, ranjit).
father(marshall, wendy).
father(ranjit, stellar).
father(tony, abby).
father(tony, swarley).
father(tony, victoria).
father(steve, chuck).
father(steve, ellie).
father(chuck, john).
father(devon, morgan).
mother(robin, barney).
mother(robin, ranjit).
mother(lily, wendy).
mother(wendy, stellar).
mother(stellar, abby).
mother(stellar, swarley).
mother(stellar, victoria).
mother(carina, chuck).
mother(carina, ellie).
mother(sarah, john).
mother(ellie, morgan).
现在,我的谓词:
parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
son(X,Y) :-
male(X),
parent(Y,X).
daughter(X,Y) :-
female(X),
parent(Y,X).
sibling(X,Y) :-
parent(Z,X),
parent(Z,Y).
cousin(X,Y) :-
parent(Z,X),
parent(W,Y),
parent(G,Z),
parent(G,W).
ancestor(X,Y) :-
parent(X,Z),
ancestor(Z,Y).
ancestor(X,Y) :- parent(X,Y).
notmember(X,[]).
notmember(X,[H|T]) :-
X \= H,
notmember(X,T).
connected(X,Y,_) :- X == Y.
connected(X,Y,Visited) :-
ancestor(X,Z),
notmember(Z,Visited),
connected(Z,Y,[Z|Visited]).
connected(X,Y,Visited) :-
ancestor(Z,X),
notmember(Z,Visited),
connected(Z,Y,[Z|Visited]).
connected(X,Y,Visited) :-
sibling(X,Z),
notmember(Z,Visited),
connected(Z,Y,[Z|Visited]).
connected(X,Y,Visited) :-
married(X,Z),
notmember(Z,Visited),
connected(Z,Y,[Z|Visited]).
connected(X,Y) :- connected(X,Y,[X]).
minimum(X,[X]).
minimum(X,[M,H|T]) :-
M =< H,
minimum(X,[M|T]).
minimum(X,[M,H|T]) :-
M > H,
minimum(X,[H|T]).
distance(X,X,_,0).
distance(X,Y,Visited,N) :-
parent(X,Z),
notmember(Z,Visited),
distance(Z,Y,[Z|Visited],N1),
N is N1+1.
distance(X,Y,Visited,N) :-
parent(Z,X),
notmember(Z,Visited),
distance(Z,Y,[Z|Visited],N1),
N is N1+1.
distance(X,Y,N) :- distance(X,Y,[],N).
编辑:谢谢,我想我现在已经设法解决了一半的问题。接受@twinterer 的建议,我已经修复了这样的谓词
connected(X,Y,_) :- X == Y.
connected(X,Y,V) :-
married(X,Z),
notmember(Z,V),
connected(Z,Y,[Z|V]),!.
connected(X,Y,V) :-
sibling(X,Z),
notmember(Z,V),
connected(Z,Y,[Z|V]),!.
connected(X,Y,V) :-
parent(X,Z),
notmember(Z,V),
connected(Z,Y,[Z|V]),!.
connected(X,Y,V) :-
parent(Z,X),
notmember(Z,V),
connected(Z,Y,[Z|V]),!.
connected(X,Y) :- connected(X,Y,[X]).
minimum(X,[X]).
minimum(X,[M,H|T]) :-
M =< H,
minimum(X,[M|T]).
minimum(X,[M,H|T]) :-
M > H,
minimum(X,[H|T]).
count(X,[],0).
count(X,[X|T],N) :-
count(X,T,N1),
N is N1+1.
count(X,[H|T],N) :-
X \== H,
count(X,T,N1),
N is N1.
distance(X,X,Visited,0) :-
count(X,Visited,N),
N =< 1, !.
distance(X,Y,Visited,N) :-
parent(X,Z),
(notmember(Z,Visited)->
distance(Z,Y,[Z|Visited],N1),
N is N1+1
;
fail
),!.
distance(X,Y,Visited,N) :-
parent(Z,X),
(notmember(Z,Visited)->
distance(Z,Y,[Z|Visited],N1),
N is N1+1
;
fail
),!.
distance(X,Y,N) :-
findall(N1,distance(X,Y,[X],N1),L),!,
minimum(N,L),!.
但是现在出现了一系列新问题
- 它不能接受任意查询,例如
distance(X,y,n)
- 类似
connected(X,y)
返回重复结果的查询
我认为可以通过使用该findall/3
谓词来删除重复的结果,但我对如何实际实现它一无所知。