我目前正在编写一个铁路线程序,但在使用来自事实的列表时遇到了一些麻烦。我对 Prolog 很陌生,到目前为止已经写了以下事实和规则:
location(euston, [northernLine]).
location(warrenStreet, [victoriaLine, northernLine]).
location(warwickAvenue, [bakerlooLine]).
location(paddington, [bakerlooLine]).
hasCommonLine(Location1, Location2, Line) :-
location(Location1, Line),
location(Location2, Line).
这个想法是让规则返回两个位置共有的行的名称。如果我尝试的话这会起作用hasCommonLine(warwickAvenue,paddington,Line).
,但是如果我尝试它会返回 falsehasCommonLine(euston,warrenStreet,Line).
.
我怀疑这是因为该规则仅检查列表的第一个元素,因此仅比较[northernLine]
and [victoriaLine]
而不是检查列表中的每个元素。任何实现这一目标的指导将不胜感激!