1
votes

I have some values from a knowledge base with 3 fields: code, date and amount. I want to add all amount selected by date and code.

I have this code

%code, date, amount
values('AAA', date(02, 03, 2020), 1000).
values('AAA', date(31, 03, 2020), 2000).
values('AAA', date(02, 04, 2020), 1350).
values('BBB', date(15, 04, 2020), 1500).
values('CCC', date(15, 05, 2020), 950).

add_amount(Code, Month, Result) :- values(Code, date(_, Month, _), Amount).

but i don't know how select several values. Can someone give me an advice?

1
Hint: take a look at findall/3. - Willem Van Onsem

1 Answers

1
votes
?- MONTH=04 , findall(AMOUNT,values(_,date(_,MONTH,_),AMOUNT),AMOUNTs) .
MONTH = 4,
AMOUNTs = [1350, 1500].

?- CODE='AAA' , findall(AMOUNT,values(CODE,date(_,_,_),AMOUNT),AMOUNTs).
CODE = 'AAA',
AMOUNTs = [1000, 2000, 1350].

?-