【问题标题】:Prolog program that lists facts on who is a parent of who, and who is female and who is maleProlog 程序列出了谁是谁的父母、谁是女性、谁是男性的事实
【发布时间】:2019-02-22 16:54:29
【问题描述】:

我无法理解如何在 Prolog 中启动这个程序。

目标: 编写一个 Prolog 程序,列出关于谁是谁的父母、谁是女性以及谁是男性的事实。基于这些事实,它还编写了关于什么定义母亲和什么定义父亲的规则。但是,实际上不能使用mother和father函数。

例子:

?- mother(X, tommy)
X = jane
?- father(X, tommy)
X = john
?- mother(X, tammy)
X = beth
?- father(X, tammy)
X = mike

感谢任何帮助。我该怎么做呢?

【问题讨论】:

    标签: list prolog


    【解决方案1】:

    这是 prolog 中的一个非常常见的练习,只需快速 google 一下,就有无数类似的解决方案可以满足您的需求!让我们将示例提供的信息弹出到一组事实中。

    parent(jane, tommy).
    parent(beth, tammy).
    parent(john, tommy).
    parent(mike, tammy).
    
    female(jane).
    female(beth).
    
    male(john).
    male(mike).
    

    事实中不能使用mother和father函数。

    所以我们必须想办法解决这个问题。所以我们知道,如果你是母亲,你必须是女性,如果你是父亲,你必须是男性。

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

    所以当你写father(X, tammy). 时,它会返回X = mike。 我建议您深入了解this github,因为该方法非常相似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 2017-06-11
      相关资源
      最近更新 更多