我有一个数据库看起来像;
airport(ist, 90).
airport(saw, 45).
airport(esb, 60).
airport(adb, 60).
airport(erz, 30).
airport(ayt, 90).
airport(mlx, 30).
airport(tzx, 30).
airplane(f1, ist, [esb,tzx,saw]).
airplane(f2, ist, [mlx,esb,erz,esb]).
airplane(f3, ist, [esb,ist,esb,ist]).
airplane(f4, saw, [ayt,saw,ayt,saw]).
airplane(f5, erz, [esb,erz,esb]).
airplane(f6, mlx, [ist,esb,tzx,saw]).我有一个名为“two”的谓词,它接受两个列表作为参数。所以,如果你写了testing(ist,X)。你应该得到X=esb,mlx。我写了这段代码。
testing([],[]).
testing([D|D1],[L|L1]) :-
airport(D,_),
airplane(_,D,[L|_]),
testing(D1,L1).这是可行的,输出是:
[8] 60 ?- listConnections([ist],X).
X = [esb] ;
X = [mlx] ;
X = [esb].但这不是我想要的。所以第一个问题是我需要一个像X=esb,mlx这样的单行答案。第二个问题是列表中不应该有重复的元素。我希望我的问题是清楚的。任何帮助都将不胜感激。
发布于 2012-04-23 23:48:23
您可以使用setof/3
testing_set(List, Result) :-
setof(L, testing(List, L), Result).您可能感兴趣的一个页面是this SWI-Prolog documentation page。
请注意,它返回的不是[esb,mlx],而是[[esb], [mlx]],不过很容易修复。
https://stackoverflow.com/questions/10283798
复制相似问题