我有:
mymake(Answer_Max):-
findall((Place, Cost), costOfLiving(Place, Cost), ResultList),
delete_over(ResultList, Answer_Max).
Run Code Online (Sandbox Code Playgroud)
costOfLiving 在我的数据库中,由每个地方和成本组成,例如:
costOfLiving(germany, 500).
costOfLiving(france, 500).
Run Code Online (Sandbox Code Playgroud)
等等.所以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).
Run Code Online (Sandbox Code Playgroud)
我怎么能解决它才能得到我想要的东西?(如果出现其他问题)
用我的解决方案编辑 (并提供帮助)
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).
Run Code Online (Sandbox Code Playgroud)
您可以过滤结果findall/3.顺便说一句,mymake应该有第二个答案.
costOfLiving(germany, 500).
costOfLiving(france, 500).
mymake(Answer_Max, ResultList) :-
findall( (Place, Cost)
, ( costOfLiving(Place, Cost)
, Cost >= Answer_Max
)
, ResultList
).
Run Code Online (Sandbox Code Playgroud)
最后:
?- mymake(100,X).
X = [ (germany, 500), (france, 500)].
?- mymake(600,X).
X = [].
Run Code Online (Sandbox Code Playgroud)