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?
3
Answers
One easy way to achieve this is using
setof/3
which eliminates the duplicates from the list of solutions:There is a difference between this and your query, as in this one all solutions are being put in a list, not given one at a time. But you can use
member/2
to get the same behaviour:Edit : false’s answer is a better solution as it doesn’t build the unnecessary list
All
.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: