【问题标题】:Adding dynamical amount of numbers添加动态数量的数字
【发布时间】:2014-11-24 17:06:52
【问题描述】:

取一个给定的数据库,例如

input(80).
input(30).
input(25).
input(90).

计算超过 50 乘以 100 的输入量,限制为仅采用 /1 输入。 例如

%compute(?integer).
compute(I).
I = 200 %seeing as input(80) and input(90) matches the condition of being above 50

我尝试了以下 prolog 代码来模仿计算功能,但未成功:

compute(I) :- input(G), G>50, I is I+100.

I+100 没有按我的意愿工作。

【问题讨论】:

  • 您的compute 不起作用,因为逻辑表明,如果G 是输入、G > 50I is I + 100,*compute(I) 为真。 I 没有初始值,如果有,则第一次成功一次G > 50。您真的想要聚合所有可能的值,这可以使用 findall 来完成。

标签: prolog logic logic-programming


【解决方案1】:

Prolog 正在逐一搜索匹配项,并返回每个输入的查询结果,而不是所有输入。要收集所有匹配值,您可以使用bagofsetoffindall 元谓词。 这是执行您定义的代码:

input(80).
input(30).
input(25).
input(90).

compute(I) :- 
    findall(X, (input(X), X>50), L), % Find all X's that are 'input' and >50 into L
    length(L,Len),                  % Find the length of L and put into Len
    I is Len * 100.                 % I is Len times 100

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-03
    • 2016-09-15
    • 2016-09-13
    • 2011-08-28
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多