-1

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

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

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

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

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

4

1 回答 1

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
于 2014-11-24T17:25:56.287 回答