我目前正在尝试回答一段课程。正如标题所示,我需要创建一个规则来说明哪些电台位于特定线路上。我已经输入了前面脚本中所需的数据,并且一直在尝试使用findall
函数。显然有些事情出了问题,你能指导我到哪里出错吗?
请不要给我一个完整的答案。
以下是代码段。
station(kennington,northern).
station(embankment,northern).
station(tottenhamcourtroad,northern).
station(warrenstreet,northern).
station(euston,northern).
/*Q3 which stations are on a particular Underground line? */
line(Line, ListOfStations) :-
findall(Line,X),
findall(X,ListOfStations).
答案 0 :(得分:2)
您正在寻找的findall的用法是findall(+Template, :Goal, -Bag)
(正如Guy Coder所说:findall/3)。
这意味着找到满足:Goal
所写的每组变量(在这个例子中,您正在寻找station(X,Line)
),然后将它们放在-Bag
列表中(您使用ListOfStations
(对您而言,大概是+Template
来致电X
)来创建[X1,X2,X3|...]
。)
把它们放在一起你得到(扰乱警告,我猜?):
findall(X, station(X,Line), ListOfStations).
将输出:
[kennington, embankment, tottenhamcourtroad, warrenstreet, euston]
按照事实的顺序排列清单。
其他信息:
如果您的模板看起来像[X]
而不仅仅是X
:
findall([X], station(X,Line), ListOfStations).
您的输出如下所示:
[[kennington], [embankment], [tottenhamcourtroad], [warrenstreet], [euston]]
你甚至可以让它看起来像(station,X)
:
findall((station,X), station(X,Line), ListOfStations).
给你:
[(station,kennington), (station,embankment), (station,tottenhamcourtroad), (station,warrenstreet), (station,euston)]
如果你想在一条线上找到每个台的组合,:Goal
也可以是复合的,如下所示:
findall((X,Y), (station(X,Line),station(Y,Line),X\=Y), ListOfStations).
给你这样的傻话:
[(kennington,embankment), (kennington,tottenhamcourtroad), (kennington,warrenstreet), (kennington,euston), (embankment,kennington), (embankment,tottenhamcourtroad), (embankment,warrenstreet), (embankment,euston), (tottenhamcourtroad,kennington), (tottenhamcourtroad,embankment), (tottenhamcourtroad,warrenstreet), (tottenhamcourtroad,euston), (warrenstreet,kennington), (warrenstreet,embankment), (warrenstreet,tottenhamcourtroad), (warrenstreet,euston), (euston,kennington), (euston,embankment), (euston,tottenhamcourtroad), (euston,warrenstreet)]
我希望能让你对findall的强大功能有所了解。