1
votes

I'm new to prolog and I'm trying to write a predicate that returns "Yes" if two train stations are on the same line.

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).

Example: ?- same_line(n,j). Yes

However, I get this error in WIN-PROLOG when compiling: ! Error 67 : Predicate Protected

What am I doing wrong?

1
Why do you have semicolons (;) at the end of your line assertions? They should be periods (.).lurker
Thanks. Like I said, I'm new to prolog. That's kind of a "reflex" to use (;) at the end of a line.SaintLike
Does that solve the problem?lurker
yeah, it did. I feel stupid, about that.SaintLike
No worries. I bop between Prolog, Ruby, Python, and C/C++ and it happens to me, too...lurker

1 Answers

0
votes

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