【问题标题】:Family tree in PrologProlog中的家谱
【发布时间】:2017-07-26 13:46:51
【问题描述】:

当我试图查看谁是谁的兄弟和姐妹时,它给了我儿子和女儿,我找不到错误......

    father(pedro-i,fernando-i).
    father(pedro-i,beatriz(1347)).
    father(pedro-i,joão(1349)).
    father(pedro-i,dinis(1354)).
    father(pedro-i,joão_grão_mestre_da_ordem_de_avis).
    mother(constança(1320),luis).
    mother(constança(1320),maria(1342)).
    mother(constança(1320),fernando-i).
    mother(inês_de_castro,beatriz(1347)).

任何其他我欣赏的意见

    ancestor(X,Y) :- mother(X,Y).
    ancestor(X,Y) :- father(X,Y).

    if_then_else(X,Y,male) :- father(X,Y).
    if_then_else(X,Y,female) :- mother(X,Y).

    son(X,Y) :- father(X,Y).
    sister(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,female).
    brother(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,male).
    descendent (X,Y) :- filho(X,Y). 
    descendent (X,Y) :- filho(X,Z),descendent (Z,Y).
    grandfather(X,Y) :- ancestor(X,Z),ancestor(Z,Y).
    grandmother(X,Y) :- ancestor(X,Z),ancestor(Z,Y).
    grandchildren(X,Y) :- ancestor(Z,X),ancestor(Y,Z).

    uncle(X,Y) :- brother(X,Z),ancestor(Z,Y).

【问题讨论】:

  • 您输入什么查询?你得到什么结果?你期待什么结果?
  • 例如,如果我输入以下查询:'brother(X,fernando-i)。'我得到:'X=pedro-i',我想得到:'X=joão(1349)。 X=迪尼斯(1354)。 X=joão_grão_mestre_da_ordem_de_avis) ' 只有男性,如果我问姐姐只有女性@lurker
  • s/pedro-i/pedro_i/g
  • @false 不明白你想说什么。
  • 不要像pedro-i那样在名称中直接使用-,而是使用_。如果您真的坚持,请将整个名称放在引号中,例如'pedro-i'

标签: prolog family-tree


【解决方案1】:

您的子句brother(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,male). 要求 Y 有一个祖先,但 X 也需要有一个祖先——相同的祖先:

brother(X,Y) :- ancestor(Z,Y),ancestor(Z,X), X\==Y,if_then_else(X,Y,male).

您还需要消除最后 X 是 Y 的父亲的要求。

brother(X,Y) :- ancestor(Z,Y),ancestor(Z,X), X\==Y,male(X).

male需要简单的依赖个人(你不需要是父亲才能成为男性。)male (fernando-i).

【讨论】:

    猜你喜欢
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多