0

我是 prolog 的新手,我正在尝试编写一个谓词,如果两个火车站在同一条线上,则返回“是”。

line(1,[a,b,c,d,e]);
line(2,[b,g,f,e,i,h]);
line(3,[l,m,g,n,f,o,p,q,i,j]);


same_line(X,Y):-
    line(_,L),
    member(X,L),
    member(Y,L).

示例:?-same_line(n,j)。是的

但是,我在编译时在 WIN-PROLOG 中收到此错误:!错误 67:谓词受保护

我究竟做错了什么?

4

1 回答 1

0

Just answering, so that the question goes off the list of unanswered questions: Yes the (;)/2 is a problem. Further if you really only want a yes or no, you could also try memberchk/2 instead of member/2. The code would read:

line(1,[a,b,c,d,e]).
line(2,[b,g,f,e,i,h]).
line(3,[l,m,g,n,f,o,p,q,i,j]).

same_linechk(X,Y):-
    line(_,L),
    memberchk(X,L),
    memberchk(Y,L).

And works as expected:

?- same_linechk(n,j).
true.

The predicate memberchk/2 is part of SWI-Prolog and does not backtrack after the first time it has found a matching member. It is essentially a menber/2 with a builtin cut and thus in the average twice as fast.

Bye

于 2014-05-18T13:07:40.973 回答