XSB prolog: Problems with lists

user3033489

I'm new to XSB prolog and I'm trying to solve this problem.

I've got prices of products and some orders. It looks like this:

price(cola,3).                         
price(juice,1).          
price(sprite,4).
// product for ex. is cola with a price of 3 (something, it doesn't matter which currency)

order(1, [cola,cola,sprite]).   
order(2, [sprite,sprite,juice]).    
order(3, [juice,cola]).     // the number here is the number of the order
                            // and the list represents the products that
                            // belong to that order

Now, my task is to write a new function called bill/2. This function should take the number of the order and then sum up all the prices for the products in the same order(list).

Something like:

|?-bill(1,R). 

R= 10 ==> ((cola)3 + (cola)3 + (sprite)4 = 10)             

|?-bill(2,R).

R= 9   ==> ((sprite)4 + (sprite4 + (juice)1 = 9) 

and so on... I know how to get to the number of the order but I don't know how to get each product from the list inside that order to get to it's price, so I can sum it up.

Thanks in advance.

CapelliC

In plain Prolog, first get all numbers in a list, then sum the list:

bill(Ord, Tot) :-
   order(Ord, Items),
   findall(Price, (member(I, Items), price(I, Price)), Prices),
   sum_list(Prices, Tot).

but since XSB has tabling available, there could be a better way, using some aggregation function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related