Pizzeria提供五种比萨饼:
约翰,安娜和凯特遵循以下命令:
计算:
我做了什么:
% facts
pizza(hawaii, 25.90).
pizza(capricciosa, 20.90).
pizza(margherita, 9.90).
pizza(pepperoni, 27.90).
pizza(wege, 14.90).
% separate pizzas with persons (1)
person(john, hawaii).
person(john, pepperoni).
person(anna, capriciosa).
person(anna, margherita).
person(kate, hawaii).
% or list of pizzas with persons? (2)
person(john, [hawaii, pepperoni]).
person(anna, [capriciosa, margherita]).
person(kate, [hawaii]).
% predicates
% In simple excercises I don't have problems, but now I don't know what to do.
我的问题是我可以(或应该)做些什么来计算这些事情。
% this returns cost of pizza ordered by Person
cost(Person, Cost) :- person(Person, Pizza), pizza(Pizza, Cost)
我想做什么:
提问:
?- each_paid(john, Cost).
Cost = 53.8.
我认为我应该做什么:
- 从事实名单中获取披萨
- 获取披萨的价格并添加到费用
- 获取下一个披萨并转到2
醇>
% (1)
% person(john, hawaii).
% person(john, pepperoni).
each_paid(Person, Cost) :- person(Person, Pizza), pizza(Pizza, Cost2), add Cost2 to Cost and find another pizza
或
% (2)
% person(john, [hawaii, pepperoni]).
each_paid(Person, Cost) :- person(Person, [Head|Tail]), pizza(Head, Cost2)
将Cost2
添加到Cost
并找到另一个披萨
(2)当尝试查找不存在的person(john, [pepperoni])
并且还需要(1)事实时,会发生错误。