# University exam question

Dear swi prolog community,

I have an assignment task for my university exam in prolog. If you can help me in anyway, i would appreciate it.

This is an information about age and population of capitals: Country, City, Age, Population

Estonia, +372, Tallinn, 1154, 444532
Latvia, +371, Riga, 1201, 614618
Lithuania, +370, Vilnius, 1323, 588412

Task 1 - is to describe a predicate for consultation with this base to get exactly the same list.

Task 2 - this predicate return only nations with capitals older than a certain age, or where more than a certain number of people living.

Kindly provide me solution.

Donâ€™t expect others to do your homework for you. Explain why you are having any difficulty, being as specific as you can, and you will find people happy to explain general prolog principles which will help you solve specific problems.

bagof/3 and member/2 are inverses.

``````p('Estonia', '+372', 'Tallinn', 1154, 444532).
p('Latvia', '+371', 'Riga', 1201, 614618).
p('Lithuania', '+370', 'Vilnius', 1323, 588412).
``````

And try with these queries using the REPL:

``````?- bagof(p(Country, Something, City, Age, Population),
p(Country, Something, City, Age, Population),
List).
List = [p('Estonia', '+372', 'Tallinn', 1154, 444532),
p('Latvia', '+371', 'Riga', 1201, 614618),
p('Lithuania', '+370', 'Vilnius', 1323, 588412)].
``````
``````?- member(P, [p('Estonia','+372','Tallinn',1154,444532),
p('Latvia','+371','Riga',1201,614618),
p('Lithuania','+370','Vilnius',1323,588412)]).
P = p('Estonia', '+372', 'Tallinn', 1154, 444532) ;
P = p('Latvia', '+371', 'Riga', 1201, 614618) ;
P = p('Lithuania', '+370', 'Vilnius', 1323, 588412).
``````
1 Like

Thank you ! but how to write queries with REPL ?

Wikipedia explains REPL.

Anyway, hereâ€™s a brief session using Linux; although judging from your response, I suspect I havenâ€™t given you a useful answer. The lines that start with â€ś\$â€ť are command line prompts from the shell; the lines that start with â€ś?-â€ť are prompts for query from SWI-Prolog; the ";"s were entered by the user, to get additional results that satisfy the query.

``````\$ cat p.pl
p('Estonia', '+372', 'Tallinn', 1154, 444532).
p('Latvia', '+371', 'Riga', 1201, 614618).
p('Lithuania', '+370', 'Vilnius', 1323, 588412).

list_facts(List) :-
bagof(p(Country, Something, City, Age, Population),
p(Country, Something, City, Age, Population),
List).

fact(P) :-
member(P, [p('Estonia','+372','Tallinn',1154,444532),
p('Latvia','+371','Riga',1201,614618),
p('Lithuania','+370','Vilnius',1323,588412)]).

\$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.9)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- [p].
true.

?- p(Country, Something, City, Age, Population).
Country = 'Estonia',
Something = '+372',
City = 'Tallinn',
Age = 1154,
Population = 444532 ;
Country = 'Latvia',
Something = '+371',
City = 'Riga',
Age = 1201,
Population = 614618 ;
Country = 'Lithuania',
Something = '+370',
City = 'Vilnius',
Age = 1323,
Population = 588412.

?- list_facts(List).
List = [p('Estonia', '+372', 'Tallinn', 1154, 444532), p('Latvia', '+371', 'Riga', 1201, 614618), p('Lithuania', '+370', 'Vilnius', 1323, 588412)].

?- fact(P).
P = p('Estonia', '+372', 'Tallinn', 1154, 444532) ;
P = p('Latvia', '+371', 'Riga', 1201, 614618) ;
P = p('Lithuania', '+370', 'Vilnius', 1323, 588412).

?-

% halt
\$
``````

Hi sir ! thank you for your brief explanation. I have tried to to run code on swi prolog online, I am getting following error

procedure `(?-A)â€™ does not exist

`[p].` imports file `p.pl`.

I canâ€™t help you any more â€“ your instructor should have given you this very basic information.