【问题标题】:How to return all facts that are less than or equal to an input value in prolog如何在prolog中返回小于或等于输入值的所有事实
【发布时间】:2020-09-08 10:16:43
【问题描述】:

我是 Prolog 的新手,正在努力解决这个问题。我有一个作者、书籍和价格列表:

wrote(applying_uml,pauline_wilcox).
wrote(applying_uml,pooley_rob).
wrote(artificial_intelligence,boden_margaret).
wrote(artificial_vision,cantoni_virginio).
wrote(artificial_vision,levialdi_stefano).
wrote(bioinformatics,critchlow_terence).
price(applying_uml,72).
price(artificial_intelligence,92).
price(artificial_vision,48).
price(bioinformatics,57).

我正在尝试编写一个谓词,在给定输入价格的情况下,将返回所有作者的书籍和价格,直至输入价格。到目前为止,我有这个:

specs(BK1, Aut1, P1) :- price(BK1, P1), wrote(BK1, Aut1).

其中 P1 是输入的价格,BK1 和 AUT1 输出具有该价格的书籍及其作者。但我不知道如何让它也返回成本低于输入价格的书籍及其作者。例如,如果我输入 57 作为 P1,它应该返回人工视觉和生物信息学,如下所示:

48 pounds
BK1 = artificial_vision,
Aut1 = cantoni_virginio ;
48 pounds
BK1 = artificial_vision,
Aut1 = levialdi_stefano ;
57 pounds
BK1 = bioinformatics,
Aut1 = critchlow_terence.

我尝试使用第二个价格变量 P2 并使其

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange (SE) 网络上发帖,您已根据 CC BY-SA license 授予 SE 分发内容的不可撤销权利(即无论您未来的选择如何)。根据 SE 政策,分发非破坏版本。因此,任何破坏行为都将被撤销。请参阅:How does deleting work? …。如果允许删除,则帖子下方左侧有一个“删除”按钮,但仅在浏览器中,而不是移动应用程序中。

标签: prolog predicate comparison-operators


【解决方案1】:

你可以这样做:

find_book_under(PMax,BK1, Aut1, P1) :-
   specs(BK1, Aut1, P1),
   PMax >= P1.

您的问题不清楚为什么这对您不起作用。如果您使用 PMax 的特定值查询它,则此子句有效。此外,必须先进行比较,因为必须实例化变量才能成功。

【讨论】:

    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2018-08-09
    • 2015-09-04
    • 2016-09-30
    相关资源
    最近更新 更多