繁体   English   中英

Prolog 规则和查询

[英]Prolog rules and query

我需要一些帮助来查找规则和/或查询 Prolog 中的知识库,其中包含有关超市中客户的信息。

例如我有:

Customer(Name,Age,Sex,Wage).

customer(John,30,M,2000).
customer(Mary,35,F,2500).
customer(Mark,40,M,2500).

invoice(Number, CostumerName, Product, Price).

invoice(001, John, Potatoes, 20).
invoice(002, John, Tomatoes, 10).
invoice(003, Mary, Soap, 50).
invoice(004, Mark, Potatoes, 20).
invoice(005, Mary, Detergent, 15).

Food(Potatoes).
Food(Tomatoes).
CleanProd(Soap).
CleanProd(Detergent).

例如,如果我想找到一个趋势,以了解女性比男性购买的清洁产品更多......我应该使用哪种或什么规则和查询?

您可以使用findall后跟sort来收集查询的唯一结果(您不希望列表中有重复的发票),并使用length来检查收集的结果的长度。

例如:

findall(X, (invoice(X, C, P, _), customer(C, _, f, _), cleanProd(P)), Xs),
sort(Xs, InvoicesOfWomenBuyingCleanProducts),
length(InvoicesOfMenBuyingCleanProducts, N).

另外,请注意变量以大写开头,原子以小写开头(也适用于谓词名称)。

如果你真的想要一个以大写开头的原子,你必须用单引号括起来。

变量: M

原子: 'M'

原子: m

例如,你可以用这种方式重写你的知识库:

%   this is not needed, however is useful as
%   a documentation of what your predicate means:
% customer(Name,Age,Sex,Wage).

customer('John',30,m,2000).
customer('Mary',35,f,2500).
customer('Mark',40,m,2500).

% invoice(Number, CostumerName, Product, Price).

invoice(001, 'John', potatoes, 20).
invoice(002, 'John', tomatoes, 10).
invoice(003, 'Mary', soap, 50).
invoice(004, 'Mark', potatoes, 20).
invoice(005, 'Mary', detergent, 15).

food(potatoes).
food(tomatoes).
cleanProd(soap).
cleanProd(detergent).

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM