【问题标题】:Prolog:simple family relation序言:简单的家庭关系
【发布时间】:2012-10-23 16:06:38
【问题描述】:

我正在学习 Prolog,我想要的是对一个简单的家庭成员进行简单的“计算”(我不知道它在 Prolog 中是如何称呼的)。

例如我有:

 1)father of steve is petter
 2)brother steve is john
 3)A person is a son to a Father when the brother of the person has  as father the Father

(当它不在我的脑海中时,它似乎很有趣并且完全不合逻辑:))

father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).

我的问题是约翰的父亲是谁(正确的遮阳篷应该是彼得)

?-father(john,X).

但它总是给我假。

【问题讨论】:

    标签: prolog logic family-tree


    【解决方案1】:

    当您输入father(john, X). 时,它首先尝试查找Z,使得brother(john, Z) 为真。不存在这样的Z,所以它返回false。

    请注意,brother(steve, john) 并不意味着 brother(john, steve),除非您告诉 Prolog 它应该这样做。

    【讨论】:

    • Well.in the tutorial我正在阅读的东西(1,2)说可以被解释为东西(2,1),但我想这只是程序员而不是Prolog的意思......我的错......但即使我改变它们,我也会再次变得虚假,即使我把这两个案例都放在了。什么是正确的方法?
    • @SteveL 我认为您阅读的内容意味着您可以将Mike plays with a dog 之类的短语编码为plays(mike, dog)plays(dog,mike)。但是一旦你选择了一种解释,你就必须坚持下去。
    【解决方案2】:

    已解决:

    father(steve,petter).
    brother(john,steve).
    whoisfather(X,Y):-brother(X,Z),father(Z,Y).
    ?- whoisfather(john,X).
    X = petter.
    

    而不是

    father(steve,petter).
    brother(john,steve).
    father(X,Y):-brother(X,Z),father(Z,Y)).
    ?- father(john,X).
    false.
    

    见cmets

    【讨论】:

    • 不,实际上,您的原始代码有效。你只有一个额外的括号,一个错字。当我运行它时,我得到2 ?- father(john,X). ==> X = petter ;(这里我按;)然后是No
    • 大声笑。我怎么会错过那个 omg。这个 *** 小错误让你发疯。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多