I have the map of Romania from the Russell and Norvig book about artificial intelligence. I created links between the cities like this:
link(oradea, zerind, 71).
link(oradea, sibiu, 151).
link(zerind, arad, 75).
link(arad, sibiu, 140).
link(arad, timisoara, 118).
link(timisoara, lugoj, 111).
link(lugoj, mehadia, 70).
link(mehadia, drobeta, 75).
link(drobeta, craiova, 120).
I want to find the cities that connect with Oradea or Arad but when I ask this:
(link(X, arad, _); link(arad, X, _));(link(X, oradea, _); link(oradea, X, _)).
It returns:
X = zerind ;
X = sibiu ;
X = timisoara ;
X = zerind ;
X = sibiu.
How can I make it return a solution only once?
Another way to get your solutions only once is to dynamically use the database to store the solutions when you find them:
Observations:
The solution with
setof/3
is better and it should be preferred.In order to avoid leaving the database with garbage facts (undesired side effects), you can clean them all in the end: