-2

I am trying to run this prolog code in DrRacket: http://www.anselm.edu/homepage/mmalita/culpro/graf1.html

#lang datalog

arc(a,b).
arc(b,c).
arc(a,c).
arc(a,d).
arc(b,e).
arc(e,f).
arc(b,f).
arc(f,g).   

pathall(X,X,[]).
pathall(X,Y,[X,Z|L]):- arc(X,Z),pathall(Z,Y,L).       % error on this line; 

pathall(a,g)?

However, it is giving following error:

read: expected a `]' to close `['

I suspect '|' symbol is not being read as head-tail separator of the list. Additionally, [] is also giving error (if subsequent line is removed):

#%app: missing procedure expression;
 probably originally (), which is an illegal empty application in: (#%app)

How can these be corrected so that the code works and searches for paths between a and g ?

4

1 回答 1

2

DrRacket 中的 Datalog 模块不是 Prolog 的实现,并且您使用的语法是不允许的(有关允许的语法,请参阅手册)。

特别是不能是像列表([])这样的数据结构。要运行像上面这样的程序,您需要一个带有数据结构的 Prolog 解释器。

您可以做的是定义例如一个 predicate path,就像在您链接的示例中一样:

path(X,Y):- arc(X,Y).
path(X,Y):- arc(X,Z),path(Z,Y).

例如,询问路径是否存在,如:

path(a,g)?

或打印到某个节点的所有路径

path(X,g)?

等等

于 2016-08-30T09:50:37.723 回答