0
votes

Can someone explain how the following Prolog recursion works ?

findRoute(A,A,_).
findRoute(A,C,Path) :- 
    nextCnvZone(A,B),
    \+ member(B,Path), 
    findRoute(B,C,[B|Path]).

I can understand the second part but could not understand the first part i.e what is the first findRoute(A,A,_). doing ?

1

1 Answers

1
votes

It is the part that stop the recursion, i.e. when the first parameter equals the second the recursion stops, and returns true through all of the recursion levels if it made it to that level.

In general, it has the rule that the first parameter equals the second. (check if true in case both variables are given, assign the value of the second to the first if the second is variable and the first is given, etc.)

?- findRoute(1, 1, 5).
true
?- findRoute(1, 2, 5).
false
?- findRoute(1, X, 5).
X = 1
?- findRoute(X, 2, 5).
X = 2