我正在尝试从知识库中获取2个值
%code, date, amount
values1('AAA', date(02, 03, 2020), 1000).
values1('AAA', date(31, 03, 2020), 2000).
values1('AAA', date(02, 04, 2020), 1350).
values1('BBB', date(15, 04, 2020), 1500).
values1('CCC', date(15, 05, 2020), 950).我想得到代码和价值按月在两个列表。我的想法是使用findall两次,第一次是获取Code,第二次是获取值,但不是返回值,而是Code。
function4(Month, Value, Code) :-
findall(Code, values1(Code, date(_, Month, _), _), Code),
findall(Value, values1(Code, _, Value), Value).我是prolog的新手,如果有任何建议,我将不胜感激。
发布于 2020-11-03 12:52:22
您没有正确使用findall/3。要查看特定月份的所有Codes、Values的列表:
get_months_data1(Month, Codes, Values) :-
findall(Code, values1(Code, date(_, Month, _), _), Codes),
findall(Value, values1(_, date(_, Month, _), Value), Values).?- get_months_data1(03, C, V).
C = ['AAA', 'AAA'],
V = [1000, 2000].
?- get_months_data1(04, C, V).
C = ['AAA', 'BBB'],
V = [1350, 1500].
?- get_months_data1(05, C, V).
C = ['CCC'],
V = [950].如果您不想要像上面的['AAA', 'AAA']这样的重复条目,也可以使用setof/3。如果你想把它们Code, Value在一起,你可以拥有:
get_months_data(Month, Data) :-
findall(tr(Code, Value), values1(Code, date(_, Month, _), Value), Data).这给了我们
?- get_months_data(03, D).
D = [tr('AAA', 1000), tr('AAA', 2000)].
?- get_months_data(04, D).
D = [tr('AAA', 1350), tr('BBB', 1500)].
?- get_months_data(05, D).
D = [tr('CCC', 950)].https://stackoverflow.com/questions/64654119
复制相似问题