简体   繁体   中英

How do i get a list of elements that I extracted from a knowledge base in 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).

How do I get a complete list of all resulted X (which are the cities with the temperature below threshold)?

Use setof/3 instead!

:-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).

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM