Friday, 15 February 2019

Prolog: Get the population of cities in a country


Write a program that prints population of all the cities in a country.

cities.pl
/*Example: Delhi is a city in india */
city(delhi, india).
city(bangalore, india).
city(chennai, india).

city(guangzhou, china).
city(shanghai, china).
city(chongqing, china).
city(beijing , china).

/* Population of the cities */
population(delhi, 11034555).
population(bangalore, 8443675).
population(chennai, 4646732).

population(guangzhou, 44294245).
population(shanghai, 35965403).
population(chongqing, 25165500).
population(beijing, 22500000).

/* Get the population of cities */
getPopulation(MyCity, MyCountry, CityPopulation) :- city(MyCity, MyCountry), population(MyCity, CityPopulation).

getPopulation(City, india, Population).
Above statement return all the cities and their population in india.

getPopulation(City, china, Population).
Above statement return all the cities and their population in china.

getPopulation(City, _, Population).

Above statement reutrn all the cities and their population.


10 ?- consult(cities).
true.

11 ?- getPopulation(City, india, Population).
City = delhi,
Population = 11034555 ;
City = bangalore,
Population = 8443675 ;
City = chennai,
Population = 4646732.

12 ?- getPopulation(City, china, Population).
City = guangzhou,
Population = 44294245 ;
City = shanghai,
Population = 35965403 ;
City = chongqing,
Population = 25165500 ;
City = beijing,
Population = 22500000.

13 ?- getPopulation(City, _, Population).
City = delhi,
Population = 11034555 ;
City = bangalore,
Population = 8443675 ;
City = chennai,
Population = 4646732 ;
City = guangzhou,
Population = 44294245 ;
City = shanghai,
Population = 35965403 ;
City = chongqing,
Population = 25165500 ;
City = beijing,
Population = 22500000.




Previous                                                 Next                                                 Home

No comments:

Post a Comment