0

我有:

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超过 number的所有元素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).
4

2 回答 2

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 = [].
于 2012-05-18T11:44:43.717 回答
1

您可以直接从数据库中撤回,而无需构建列表,但如果您需要此结构,则需要进行更正:

delete_over([F|T], Max) :-
   F = (Place, Cost), ...
于 2012-05-18T14:54:14.480 回答