如何获取从Prolog知识库中提取的元素列表?


% Given a knowledge base of the form weather(city_name, temperature).,
% collect in a list the names of all cities with a temperature below a 
% given threshold (temperature < threshold), without using findall predicate.
:-dynamic weather/2.
weather(city1, 30).
weather(city2, 25).
weather(city3, 20).
get_city(T):- 
retract(weather(X, Y)),
Y < T,
writeln(X).

如何获得所有结果 X 的完整列表(哪些是温度低于阈值的城市(?

改用 setof/3

:-dynamic weather/2.
:-dynamic city/1.
weather(city1, 30).
weather(city2, 25).
weather(city3, 20).
get_city(T):- 
retract(weather(X, Y)),
Y < T,
assert(city(X)).

get_city(T, R):- 
get_city(T),
setof(X, city(X), R).

谢谢!