Home > Archive > Prolog > February 2005 > An inventory query
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
An inventory query
|
|
| kfaraz@gmail.com 2005-02-13, 8:59 am |
| hi
I've just started using prolog. I,ve started to make a model of a
retail system. I have defined the product nad the shopping cart(shown
below). One of the obvious queries is to find out the cost of each
product in the cart. The following code displays the cost correctly the
cost of each component of the cart, Now I would like to call
cost_of_cart(T,C), where T is the cart and C is the total cost of the
cart. for this how can I add the cost of the invidual products, in
order to obtain the total cost when the predicate cost_of_cart(T,C) is
called.
shopping_cart(cart1,[quant(12906,5),quan
t(7655,1), quant(4097,3)]).
cost_of_cart(T):-
shopping_cart(T,[quant(P,Y)|L]),
cost_of([quant(P,Y)|L]).
cost_of([]).
cost_of([quant(P,Y)|L]):-
product(P,D,_,_),
C is D * Y,
tab(4), write(P),put(9), write(C), nl,
cost_of(L).
the result of this program is
?- cost_of_cart(cart1).
12906 299.95
7655 489.99
4097 50.97
Yes
I am just startng of in prolog so any help in this regard will be
greatly appreciated.
| |
| tmp123 2005-02-13, 8:59 am |
| kfaraz@gmail.com wrote:
> Now I would like to call
> cost_of_cart(T,C), where T is the cart and C is the total cost of the
> cart.
>
If I understand your query, you need a "sumatory of":
| cost_of_cart([],0). /* the cost of nothing is 0 */
| cost_of_cart([quant(P,Y)|L],C) :-
| cost_of_card(L,T), /* the cost of rest */
| product(P,D,_,_),
| C is D*Y+T. /* plus the cost of head */
A few better is, sometimes (if interested, you can review any prolog
book to see why):
| cost_of_cart(L,R) :- cost_of_card(L,0,R).
|
| cost_of_cart([],UNTILNOW,UNTILNOW).
| cost_of_cart([quant(P,Y)|L],UNTILNOW,RES
ULT) :-
| product(P,D,_,_),
| UNTILNOW2 is D*Y+UNTILNOW,
| cost_of_card(L,UNTILNOW2,RESULT).
PS: Typed without test, I hope no typo mistakes.
| |
| kfaraz 2005-02-14, 12:23 am |
| Thanks for the help. I used the following structure and it works fine.
I might need some more help on something that i am working on. Could I try trouble you again?
| cost_of_cart(L,R) :- cost_of_card(L,0,R).
|
| cost_of_cart([],UNTILNOW,UNTILNOW).
| cost_of_cart([quant(P,Y)|L],UNTILNOW,RES
ULT) :-
| product(P,D,_,_),
| UNTILNOW2 is D*Y+UNTILNOW,
| cost_of_cart(L,UNTILNOW2,RESULT).
PS: Typed without test, I hope no typo mistakes.
Once again thanks |
|
|
|
|