I'm attempting to find the complement of a list, given a list L1, and a universal list L2 with the following code:
complement(L1, L2, Res):-
setof(X, (nth0(N, L2, X), not(member(X,L1))),Res).
However, my results include duplicates and are not given in list form as I'd prefer:
23 ?- complement([1,3], [-1,1,3,5,2,4,2,55,1,0], Res).
Res = [-1] ;
Res = [5] ;
Res = [2] ;
Res = [4] ;
Res = [2] ;
Res = [55] ;
Res = [0].
I figured it's probably due to Prolog's built-in backtracking but I'm not sure how to work around this to go about formatting the result correctly and getting it to remove any duplicate items in the result.