在序言中指定接穗数

时间:2018-12-23 21:13:25

标签: prolog

我找到了编写家谱树的代码,并建立了诸如父亲,母亲,儿子等的某些关系,但是我想再增加一个功能,该功能可以计算某人的所有接穗数。

有人可以帮我吗?

我不知道如何开始。

mother(teresa,ireneusz).
mother(teresa,beata).
sex(teresa,k).

son(X,Y) :- 
  sex(X,m), 
  father(Y,X),
  X\=Y.

son(X,Y) :- 
  sex(X,m), 
  mother(Y,X),
  X\=Y.

daughter(X,Y) :- 
  sex(X,k), 
  father(Y,X),
  X\=Y.

daughter(X,Y) :-
  sex(X,k),
  mother(Y,X),
  X\=Y.

kid(X,Y) :-  
  father(Y,X),
  X\=Y.

kid(X,Y) :- 
  mother(Y,X),
  X\=Y.

parent(X,Y) :- 
  kid(Y,X),
  X\=Y.

siblings(X,Y) :- 
  parent(Z,X),
  parent(Z,Y),
  X\=Y,
  X\=Z,
  Y\=X.

brat(X,Y) :- 
  sex(X,m),
  parent(Z,X),
  parent(Z,Y),
  X\=Y,
  X\=Z,
  Y\=X.

siostra(X,Y) :- 
  sex(X,k),
  parent(Z,X),
  parent(Z,Y),
  X\=Y,
  X\=Z,
  Y\=X.

wuj(X,Y) :- 
  mother(Z,Y),
  brat(X,Z),
  X\=Y,
  X\=Z,
  Y\=Z.

stryj(X,Y) :- 
  father(Z,Y),
  brat(X,Z),
  X\=Y,
  X\=Z,
  Y\=Z.

ciotka(Y,X) :- 
  mother(Z,X),
  siostra(Y,Z),
  X\=Y,
  X\=Z,
  Y\=Z.

ciotka(Y,X) :- 
  father(Z,X),
  siostra(Y,Z),
  X\=Y,
  X\=Z,
  Y\=Z.

babcia(B,A) :- 
  parent(C,A),
  parent(B,C),
  sex(B,k),
  A\=B,
  A\=C,
  B\=C.

grandfather(B,A) :- 
  parent(C,A),
  parent(B,C),
  sex(B,m),
  A\=B,
  A\=C,
  B\=C.

wife(X,Y) :- 
  sex(X,k),
  kid(Z,X),
  kid(Z,Y),
  X\=Y,
  X\=Z,
  Y\=Z.

husband(X,Y) :- 
  sex(X,m),
  kid(Z,X),
  kid(Z,Y),
  X\=Y,
  X\=Z,
  Y\=Z.

cousin(X,Y) :- 
  sex(X,m),
  kid(X,Z),
  wuj(Z,Y),
  X\=Y,
  X\=Z,
  Y\=Z.

scion(X,Y) :- 
   parent(Y,X).

scion(X,Y) :- 
   parent(Z,X),
   scion(Z,Y).

ancestor(X,Y) :- 
  kid(Y,X).

ancestor(X,Y) :- 
  kid(Z,X),
  ancestor(Z,Y).

relative(X,Y) :- 
  kid(X,Y);
  parent(X,Y);
  cousin(X,Y);
  babcia(X,Y);
  grandfather(X,Y),
  X\=Y.                    

  kid(X,Y);
  parent(X,Y);
  cousin(X,Y);
  babcia(X,Y);
  grandfather(X,Y),
  X\=Y.

1 个答案:

答案 0 :(得分:0)

您有两个选择:

  1. SWI Prolog中,您可以使用aggregate_all谓词:

aggregate_all(count, scion(X,teresa), Count).

  1. 您可以定义自己的谓词,其中包含所有可能的解决方案:

count(P,Count) :- findall(1,P,L) , length(L,Count). %and now use your predicate count(scion(X,teresa),X).