我试图在Prolog中解决一个使用否定的简单查询,但我无法破解它.查询是"查找从未销售过的类别".
知识库如下:
category(stationery, 30, 200, 10, 2).
category(books, 10, 30, 3, 2).
category(consumables, 50, 300, 15, 3).
item(pen, stationery, 10, 150).
item(colgate_small, consumables, 20, 65).
item(colgate_medium, consumables, 45, 70).
item(colgate_big, consumables, 70, 34).
item(juice_small, consumables, 45, 23).
item(juice_medium, consumables, 60, 23).
item(juice_big, consumables, 80, 12).
item(book, stationery, 5, 65).
item(pencil, stationery, 7, 56).
item(newspaper, books, 50, 400).
sale(tom, 1/1/07, pen, 3).
sale(peter, 1/1/07, book, 85).
sale(peter, 1/1/07, juice_small,1).
sale(alice, 7/1/07, pen, 10).
sale(alice, 7/1/07, book, 5).
sale(patrick, 12/1/07, pen, 7).
Run Code Online (Sandbox Code Playgroud)
可能不是最有效的方法。
not_sold(Cats) :-
findall(Y,(sale(_,_,X,_),item(X,Y,_,_)),Sold),
findall(C,(category(C,_,_,_,_),not(member(C,Sold))),Cats).
Run Code Online (Sandbox Code Playgroud)
但我认为它应该有效。