Skip to first unread message

bartek grabc

unread,
Jan 24, 2017, 11:10:02 AM1/24/17
to SWI-Prolog
Hi i have to make a prolog rule which finds all stations on a particular underground line.

I have a list of fact about each station and which line it is found on.

The format of the rule has to be :  line(Line,ListOfStations)

i am unsure of how to implement the fact database and how to proceed with making the rule in order for this to work.

i have thought of make a Fact for example, ListOfStations(central,[nh,lg,oc,tc,cl,ls,bg ]). for each of the tube lines. However one error im getting is a syntax error i do not understand what it wants me to do. Another is the the rule would look something like;

online(Line,Location):- ....some rule here... and would print out all of the stations found in that ListOfstations.

But from what i understand the user would have to enter which 'line' i want to use and the list of all the stations on that line? 

I have tried the following but it doesnt work and i do not know why... 

%Q3 : Finds Which stations are on a particular line
% A line has ListOfStations that it crosses if each station is on the
% given line.
% The rule collects each station and stores them using the setOf.
%
line(line,ListofStations) :- setof(Locations,online(Line, Locations),ListOfStations).
% A station is on a line if given Station1 on line1 is also on the line
% where the given station and station1 are teh same.
%
online(Line, Station) :- location(Location1,Line1), shareitem(Line,Line1, Line2), Location = Location1.


i dont really understand how this is supposed to work
can you help me out with getting this sorted? Note i am very new to prolog. thanks

Barb Knox

unread,
Jan 26, 2017, 12:17:06 AM1/26/17
to SWI-Prolog
On 25 Jan 2017, at 05:10, bartek grabc <supersa...@gmail.com> wrote:

Hi i have to make a prolog rule which finds all stations on a particular underground line.

I have a list of fact about each station and which line it is found on.

What is the format of this?  Do you just  have a list on paper with items like "The Warren Street station is on the Northern line", or do you have a collection of Prolog facts like  station(warren_st, northern line).  Or something else entirely?

The format of the rule has to be :  line(Line,ListOfStations)

i am unsure of how to implement the fact database and how to proceed with making the rule in order for this to work.

The fact database is exactly the collection of clauses like  station(warren_st, northern line).
The functor needn't be station, and the arguments can be in the opposite order, but that's the guts of it.
This collection of facts (which will be fairly sizeable if it's the London underground) goes in the same file as your  line/2  rule.


i have thought of make a Fact for example, ListOfStations(central,[nh,lg,oc,tc,cl,ls,bg ]). for each of the tube lines.

Don't bother.  Just have a separate fact for every existing Station,Line combination (if that's allowed by the problem spec).


However one error im getting is a syntax error i do not understand what it wants me to do. Another is the the rule would look something like;

online(Line,Location):- ....some rule here... and would print out all of the stations found in that ListOfstations.

Why not just have a rule  line(Line, Stations) :- ... that returns the list of stations for a given line?
I presume that the list does not have to be in some particular order.
Note that actually printing by you doing I/O is a rather non-Prolog-y thing to do.


But from what i understand the user would have to enter which 'line' i want to use and the list of all the stations on that line? 

No.  at the top-level (usually a "?-" prompt) you enter  line(northern line, Stations).
And the top-level will run the rule and then print the resulting list of stations.


I have tried the following but it doesnt work and i do not know why... 

%Q3 : Finds Which stations are on a particular line
% A line has ListOfStations that it crosses if each station is on the
% given line.
% The rule collects each station and stores them using the setOf.
%
line(line,ListofStations) :- setof(Locations,online(Line, Locations),ListOfStations).

% A station is on a line if given Station1 on line1 is also on the line
% where the given station and station1 are teh same.
%
online(Line, Station) :- location(Location1,Line1), shareitem(Line,Line1, Line2), Location = Location1.

Forget that  online/2.  Your existing  line/2  is fairly close to correct.  K.I.S.S.


i dont really understand how this is supposed to work
can you help me out with getting this sorted? Note i am very new to prolog. thanks

HTH.

-- 
---------------------------
|  BBB                b    \    Barbara at LivingHistory stop co stop uk
|  B  B   aa     rrr  b     |
|  BBB   a  a   r     bbb   |   ,008015L080180,022036,029037
|  B  B  a  a   r     b  b  |   ,047045,L014114L4.
|  BBB    aa a  r     bbb   |
-----------------------------


Reply all
Reply to author
Forward
0 new messages