我是XSB prolog的新手,我正试图解决这个问题。
我有产品的价格和一些订单。看起来是这样的:
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现在,我的任务是编写一个名为bill/2的新函数,这个函数应该取订单的数量,然后按照相同的顺序(列表)汇总所有产品的价格。
类似于:
|?-bill(1,R).
R= 10 ==> ((cola)3 + (cola)3 + (sprite)4 = 10)
|?-bill(2,R).
R= 9 ==> ((sprite)4 + (sprite4 + (juice)1 = 9) 等等..。我知道如何得到订单的数量,但我不知道如何从订单清单中得到每一种产品,以达到它的价格,所以我可以总结一下。
提前谢谢。
发布于 2013-11-25 18:45:36
在普通Prolog中,首先获取列表中的所有数字,然后对列表进行求和:
bill(Ord, Tot) :-
order(Ord, Items),
findall(Price, (member(I, Items), price(I, Price)), Prices),
sum_list(Prices, Tot).但是由于XSB有可用的表,所以可能有一个更好的方法,使用一些聚合函数。
https://stackoverflow.com/questions/20200580
复制相似问题