Showing posts with label Prolog program. Show all posts
Showing posts with label Prolog program. Show all posts

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

Prolog: Example Program with players information


Suppose, we had a player’s database like below.

players.pl
player(rahulDravid, 1996, 2012).
player(sachinTendulkar, 1989, 2013).
player(vvsLaxman, 1996, 2012).
player(sauravGanguly, 1996, 2008).
player(virendarSehwag, 2001, 2013).

player(rahulDravid, 1996, 2012).
Above statement tells RahulDravid played test cricket from the year 1996 to 2012.

Get the players who played in the year 1996?
Below function return the players who played in given year.
whoPlayedInTheYear(PlayerName, Year) :- player(PlayerName, StartYear, EndYear), Year >= StartYear, Year =< EndYear.

Get the players who started their career in 1996.
startedCareer(PlayerName, Year) :- player(PlayerName, StartYear, _), Year =:= StartYear.

Complete players.pl file looks like below.


players.pl
player(rahulDravid, 1996, 2012).
player(sachinTendulkar, 1989, 2013).
player(vvsLaxman, 1996, 2012).
player(sauravGanguly, 1996, 2008).
player(virendarSehwag, 2001, 2013).

/* Return the playes who played in this year */
whoPlayedInTheYear(PlayerName, Year) :- player(PlayerName, StartYear, EndYear), Year >= StartYear, Year =< EndYear.

/* Return the player who started career in this year */
startedCareer(PlayerName, Year) :- player(PlayerName, StartYear, _), Year =:= StartYear.

1 ?- consult(players).
true.

2 ?- whoPlayedInTheYear(Player, 1996).
Player = rahulDravid ;
Player = sachinTendulkar ;
Player = vvsLaxman ;
Player = sauravGanguly ;
false.

3 ?- startedCareer(Player, 1996).
Player = rahulDravid ;
Player = vvsLaxman ;
Player = sauravGanguly ;
false.



Previous                                                 Next                                                 Home