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!