First, there are no sets in Prolog. We have only lists1. So what you can do is to relate a list with duplicate elements to a list without. list_nub/2
would be such a definition.
To your current definition:
Already list2set([], [a])
succeeds, which can't be right. So your definition is too general. You need to replace list2set([],_)
by list2set([],[])
.
Then, replace member(ListH, Set)
by member(ListH,ListT)
.
And you need another rule for the case where the element is not present:
list2set([] , []).
list2set([E|Es] , Set ) :-
member(E, Es) ,
list2set(Es , Set).
list2set([E|Es] , [E|Set] ) :-
maplist(dif(E), Es),
list2set(Es , Set).
A more compact definition that avoids redundant answers is list_nub/2
.
1) Strictly speaking, one could extend unification via attributed variables2 to implement ACI-unification to have real sets.
2) To my—rough—understanding this would require the implementation of attributed variables in SICStus. Other interfaces like the current in SWI or YAP are most probably insufficient ; as they already are for CLP(B). See this discussion for more.