Friday, 15 February 2019

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

No comments:

Post a Comment