简单的序言,初学者学习

时间:2015-03-03 13:36:32

标签: prolog

我有以下序言;

customer(Invoice,_,Surname,_,Town),(Town='LA';Town='NEWYORK'),
order(Invoice,Code,_),
item(Code,_,_,_,_,_,_)

以上序言产生结果;

Invoice = 51, Surname = 'Hally', Town = 'LA', Code = 54 ;

Invoice = 51, Surname = 'Hally', Town = 'LA', Code = 58 ;

Invoice = 51, Surname = 'Hally', Town = 'LA', Code = 85 ;

Invoice = 51, Surname = 'Hally', Town = 'LA', Code = 148 ;

Invoice = 71, Surname = 'Steptoe', Town = 'NEWYORK', Code = 148 ;

Invoice = 71, Surname = 'Steptoe', Town = 'NEWYORK', Code = 176 ;

Invoice = 73, Surname = 'Ahmed', Town = 'NEWYORK', Code = 180 ;

从上面可以看出,有两个结果使用相同的代码(148),我怎样才能返回这两个结果?我尝试添加,(Code=Code).,但它不起作用。

我真的很陌生!

另外,我试图将prolog存储到一个新的谓词中,仅返回两个姓氏,如下所示

getDuplicateOrders(Surname1, Surname2):- (prolog).

但我得到一个错误,说静态变量不应该被使用!

1 个答案:

答案 0 :(得分:3)

您必须将代码分开,然后进行比较:

customer(Invoice1, _, Surname1, _, 'LA'),
customer(Invoice2, _, Surname2, _, 'NEWYORK'),
order(Invoice1, Code, _),
order(Invoice2, Code, _),    % We want the same code
item(Code,_,_,_,_,_,_).

此代码当然假定 Invoice 对于每个地区都是唯一的(例如LANEWYORK)。如果不是这种情况,那么在数据中需要另一种方式来区分不同订单的代码,如果它们确实是不同的。如果需要简单地确保发票的唯一性,dif/2将在此处起作用:

customer(Invoice1, _, Surname1, _, 'LA'),
customer(Invoice2, _, Surname2, _, 'NEWYORK'),
dif(Invoice1, Invoice2),
order(Invoice1, Code, _),
order(Invoice2, Code, _),    % We want the same code
item(Code,_,_,_,_,_,_).