0

I have a predicate directTrain/2 which defines a direct path from place A to B. Given a knowledge base, I want to define a predicate route(A,B,Route) that will give the outcome as a form of a list containing the route from A to B.

I understand that this is the same as figuring out if person A is a descend of person B, so I know that the Yes/No version of this should be something like this:

route(A,B) :-
   directTrain(A,B).
route(A,B) :-
   directTrain(A,C),
   route(C,B).

What I am lacking is the understanding of how to do this with an arity of 3, when the third variable will accumulate the final route. I want to reach the following outcome:

Given the following knowledge base ...

directTrain(ny,la).
directTrain(la,paris).
directTrain(paris,rome).

... the answer to the query ...

route(ny,rome,R).

... should be:

R = [ny,la,paris,rome]

I am very new to Prolog so I hope I will learn a lot from the answers. Thanks!

repeat
  • 18,496
  • 4
  • 54
  • 166
Chaos Monkey
  • 964
  • 1
  • 6
  • 18
  • Find the "search box" on this stackoverflow.com page, and look for "[prolog] travel route". You'll get lots of hits. – lurker Sep 05 '15 at 13:34
  • @lurker I tried that, and I found allot of answers to the Yes/No version of this question, but none of them helped me with finding the route itself, and not if there is a route. – Chaos Monkey Sep 05 '15 at 14:02
  • I'm a little surprised since this question has come up many times, including the display of the route itself. The yes/no is the answer to "if there is a route". – lurker Sep 05 '15 at 14:06
  • I just tried the search and [the very first post](http://stackoverflow.com/questions/22386947/route-planning-in-prolog) has code that gives a route. – lurker Sep 05 '15 at 14:17

0 Answers0