简体   繁体   English

如何在序言中创建结果列表?

[英]How can I create list of results in prolog?

I did write a cost calculator, the costs can be more than one value of same inputs so I need to find minimum cost. 我确实写了一个成本计算器,成本可能超过相同输入的一个值,所以我需要找到最低成本。 The calculator is running corrently, but I couldn't do put into a list and find the minumum value of the list, how can I do it? 计算器正在正确运行,但是我无法放入列表中并找到列表的最小值,我该怎么办? This is my calculator, 这是我的计算器

cal(X,Y,CLIST) :- calcualtor(X,Y,C).
calcualtor(X,Y,C ) :- calculate_cost(X,Z,A), calcualtor(Z,Y,B),C is A+B.
calcualtor(X,Y,C) :-  calculate_cost(X,Y,C).

When I call cal(A,B,X) it gives X = 7 then when I press ";" 当我调用cal(A,B,X)它给出X = 7然后按“;” it gives X = 5 then when I press ";" 它给出X = 5然后当我按“;” it gives X = 14 etc. 它给出X = 14

I want to [7,5,14,..] then find the min value of the list. 我想[7,5,14,..]然后找到列表的最小值。

Note: I can't use swi library functions 注意:我不能使用swi库函数

Why do you need a list ? 为什么需要清单? You're looking for the minimum. 您正在寻找最低要求。 That is, look for a value that does not have another greater that it. 也就是说,寻找一个没有更大的值。 The explicit negation - called \\+ - is key to force Prolog to evaluate alternative bindings. 显式否定(称为\\ + )是强制Prolog评估替代绑定的关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM