I am currently writing a rail line program but am having a little trouble using lists that come from facts. I am quite new to Prolog and so far have written the following facts and rules:
location(euston, [northernLine]).
location(warrenStreet, [victoriaLine, northernLine]).
location(warwickAvenue, [bakerlooLine]).
location(paddington, [bakerlooLine]).
hasCommonLine(Location1, Location2, Line) :-
location(Location1, Line),
location(Location2, Line).
The idea is for the rule to return the name of the line(s) that the two locations have in common. This works if I try hasCommonLine(warwickAvenue,paddington,Line).
, however it returns false if I try hasCommonLine(euston,warrenStreet,Line).
.
I suspect this is because the rule only checks the first element of the lists, therefore only compares [northernLine]
and [victoriaLine]
rather than checking every element in the list. Any guidance to accomplish this would be much appreciated!