0

I'm learning Prolog, and I'm getting creep error when I try to find a route. I think what I did is recursion because that's the way to find a route when there's not a straight path.

Here is the code:

route(london,paris).
route(paris,rome).
route(rome,spain).
route(london,berlin).
route(berlin,praga).
route(london,dublin).
route(dublin,berlin).

path(X,Y,[X,Y]):- straight(X,Y).
path(X,Z,[X | other]):- straight(X,Y), path(Y,Z,other). 

when I try to find, let's say the route from London to Rome

path(london,rome,Store).

I get this error:

Exception: (8) straight(london, rome) ? creep

Exception: (7) path(london, rome, _G4705) ? creep

What am I doing wrong? Should I define something else?

Thanks in advance!

roadrunner66
  • 7,772
  • 4
  • 32
  • 38
mary jerson
  • 51
  • 10

1 Answers1

2

Your predicate is almost correct. You just need to write the tail of the list as a variable Other instead of an atom other and secondly use your facts route/2 for a straight connection:

path(X,Y,[X,Y]) :-
    route(X,Y).
path(X,Z,[X | Other]) :-
    route(X,Y),
    path(Y,Z,Other).

Now your query works:

   ?- path(london,rome,S).
S = [london,paris,rome] ? ;
no
tas
  • 8,100
  • 3
  • 14
  • 22