0

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?

4
  • 2
    Why do you have semicolons (;) at the end of your line assertions? They should be periods (.).
    – lurker
    Sep 1, 2013 at 18:44
  • Thanks. Like I said, I'm new to prolog. That's kind of a "reflex" to use (;) at the end of a line.
    – SaintLike
    Sep 1, 2013 at 18:55
  • yeah, it did. I feel stupid, about that.
    – SaintLike
    Sep 1, 2013 at 19:48
  • No worries. I bop between Prolog, Ruby, Python, and C/C++ and it happens to me, too...
    – lurker
    Sep 1, 2013 at 23:12

1 Answer 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.