我有:
mymake(Answer_Max):-
findall((Place, Cost), costOfLiving(Place, Cost), ResultList),
delete_over(ResultList, Answer_Max).
costOfLiving
在我的数据库中,由每个地方和费用组成,例如:
costOfLiving(germany, 500).
costOfLiving(france, 500).
等等。因此ResultList
就像[(germany, 500), (france, 500), ...]
我想删除costOfLiving
超过数字Answer_Max
的数据库的所有元素,但我的delete_over无法正常工作。就像这样:
delete_over([], _).
delete_over([F|T], Max) :-
F =.. [Place, Cost], % it fails here because the F is not a list, but two atoms I think
((id_state(Place), (Cost > Max)) -> deleteState(Place) ; true),
% id_state and id_region checks that the place is defined in the database
% deleteState and deleteRegion calls a specific retractall for the database
((id_region(Place), (Cost > Max)) -> deleteRegion(Place) ; true),
delete_over(T).
我怎么能解决它才能得到我想要的东西? (如果出现其他问题)
使用我的解决方案进行编辑 (以及帮助)
mymake(Answer_Max) :- % I don't need the ResultList, but if needed just add as second parameter
findall( (Place, Cost), ( costOfLiving(Place, Cost), Cost > Answer_Max ), ResultList ),
maketolist(ResultList).
maketolist([]).
maketolist([(P,_)|T]) :- % all the elements are for deleting as their Cost is higher than the Max given
(id_state(P) -> deleteState(P) ; true), % deleteState makes a retractall according to my needs on my database
(id_region(P) -> deleteRegion(P); true), % the same for deleteRegion with regions
maketolist(T).
答案 0 :(得分:5)
您可以在findall/3
中过滤结果。顺便说一句,mymake
应该有第二个答案。
costOfLiving(germany, 500).
costOfLiving(france, 500).
mymake(Answer_Max, ResultList) :-
findall( (Place, Cost)
, ( costOfLiving(Place, Cost)
, Cost >= Answer_Max
)
, ResultList
).
最后:
?- mymake(100,X).
X = [ (germany, 500), (france, 500)].
?- mymake(600,X).
X = [].
答案 1 :(得分:1)
您可以直接从数据库中撤消,而无需构建列表,但如果您需要此结构,则需要进行更正:
delete_over([F|T], Max) :-
F = (Place, Cost), ...