the first query and duplicating elements in a list in Prolog
I am new 开发者_运维百科to Prolog. I wrote the basic code below.
flight(acompany, paris, 7).
flight(bcompany,paris,7).
flight(ccompany,paris,7).
flight(dcompany,paris,7).
search([X],Y,Z) :- flight(X,Y,Z).
search([X|T],Y,Z) :- search(T,Y,Z) , flight(X,Y,Z).
I want to do two things. First when I query as
?- search(X, paris,7).
the first thing prolog show me is
X = [acompany]
But I want to see all element in the list at the first query.
i.e
X = [acompany, bcompany, ccompany, dcompany]
And the second thing I want is avoiding duplicating the elements in the list.
For example;
X = [acompany] ;
X = [bcompany] ;
X = [ccompany] ;
X = [dcompany] ;
X = [acompany, acompany] ;
I don't want such a last list.
How can I fix these two things? Thanks.
?- setof(X, flight(X, _, _), Xs).
Xs = [acompany, bcompany, ccompany, dcompany].
精彩评论