# Prolog, I need to get the month of greatest expense

0
``````gasto_mes(enero,servicios,500).
gasto_mes(enero,impuestos,2000).
gasto_mes(febrero,servicios,700).
gasto_mes(marzo,servicios,800).
gasto_mes(marzo,impuestos,1800).
gasto_mes(abril,servicios,900).
gasto_mes(mayo,servicios,940).
gasto_mes(mayo,impuestos,1700).
``````

-After this knowledge base, I need to obtain the month of greatest expense, for which I need to obtain the sum of the expenses of the same month and save and show them. What I achieved was just to show the month of greatest expense without contemplating that the amounts are added.

``````% RECUPERA EL MES DE MAYOR GASTO. M= Mes, G=gasto, I=impuesto/servicio,
% L=lista, X=variable, R=comparador del mayor gasto, T=cola de la lista.
mayormes(M,G):- findall((M,G),gasto_mes(M,_,G),L),mayor(L,(M,G)).

%depura el mayor.
mayor([X],X).
mayor([(M,G)|T],(Mc,Gc)):- mayor(T,(M2,G2)),(G>=G2,Mc=M,Gc=G;
Mc=M2,Gc=G2),!.
``````

To explain myself better, this code I did returns that the biggest month is January with \$ 2000, when in fact the biggest month is May (940 + 1700 = 2640). Thank you very much !!!

asked by Mariel Alejandra 12.11.2018 в 02:52
source