A single query to complete my program should check if there is a direct route between two given cities. Alternatively, it can list all the connected cities for a given city. See the following:
flight(city1,city2).
flight(city2,city1).
flight(city2,city3).
flight(city3,city2).
My predicates:
route(X,Y):-
flight(X,Y).
route(X,Y) :-
flight(X,Z),
route(Z,Y).
My result :
?- route(city1,X).
X = city2 ;
X = city1 ;
X = city3 ;
X = city2 ;
X = city1 ;
X = city3 ;
X = city2 ;
X = city1 ;
X = .......
But i have infinitie recursion , and it can not stop. What can i solve this problem ?