【发布时间】:2017-01-17 13:46:18
【问题描述】:
我目前正在编写一个铁路线程序,但在使用来自事实的列表时遇到了一点麻烦。我对 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). 这会起作用,但是如果我尝试 hasCommonLine(euston,warrenStreet,Line). 它会返回 false。
我怀疑这是因为规则只检查列表的第一个元素,因此只比较 [northernLine] 和 [victoriaLine] 而不是检查列表中的每个元素。任何指导来实现这一点将不胜感激!
【问题讨论】: