Say I have two 'tables' in Prolog, e.g.:
item_value('Chair', 50).
item_value('Table', 100).
item_value('Plant', 75).
And another one:
shopping_cart('Max', ['Chair', 'Chair', 'Table']).
shopping_cart('Sam', ['Plant', 'Table']).
I now want to write a predicate that calculates the sum of the items inside the shopping cart, something like total_sum(Person, Sum)
How would i do this? I can't wrap my head around coding this in Prolog.
Thanks in advance!
Items, and you want to add up their values, you can do it easily withfindall(Cost, (member(Item, Items), item_value(Item, Cost)), Costs).then add them up,sumlist(Costs, TotalCost).. If you can't usefindall/3orsumlist/2(seems to be a favorite constraint among Prolog homework assignments), then write a recursive predicate to do it. - lurkeritem_value(Item, Cost).it will yield eachItemandCostthat is in your data. Another fundamental aspect of Prolog is handling lists. If you aren't comfortable handling lists, I'd suggest a good text book, tutorial, or look at a bunch of example problems such as 99 Prolog Problems. That's all I can give you in this context. This isn't a free, personalized language tutorial site. ;) - lurker