我在Prolog中有这个数据库,我试图归还亨利拥有的东西以及谁拥有一辆汽车和一辆卡车。我已经尝试了所有我能想到的东西来回归亨利拥有的东西,却无法找到解决方案。我知道如何归还谁拥有一辆汽车或一辆卡车? - 拥有(X,汽车(,,_)),但不是同时。任何帮助将不胜感激。
owns(bill, car(ford, mustang, 1964)).
owns(sue, car(pontiac, gto, 1967)).
owns(george, car(honda, civic, 2013)).
owns(betty, truck(ford, f150, 2013)).
owns(henry, motorcycle(honda, goldwing, 2010)).
答案 0 :(得分:4)
Prolog有一个关系数据模型,但只允许递归术语而不是像atom一样的原子,并且不为'columns'命名。松散:
╒═════════════╤═════════════╕ │ SQL │ Prolog │ ╞═════════════╪═════════════╡ │ table │ predicate │ │ record │ clause │ │ table name │ functor │ │ column │ argument │ ╘═════════════╧═════════════╛
因此,需要有关属性位置的知识。通常,我们可以在仿函数中关联属性'名称:
% get an owner of any kind listed
kinds_owner(Kinds, Owner) :-
owns(Owner, Object), Object =.. [Kind|_], member(Kind, Kinds).
% get owners of any kind listed
kinds_owners(Kinds, Owners) :-
setof(Owner, kinds_owner(Kinds, Owner), Owners).
?- kinds_owner([truck,motorcycle],X).
X = betty ;
X = henry.
?- kinds_owners([truck,motorcycle],Xs).
Xs = [betty, henry].