quick and dirty
?- S=[1,2,3,4,2,3], setof(C, R^(select(C,S,R),memberchk(C,R)), L).
S = [1, 2, 3, 4, 2, 3],
L = [2, 3].
Is a specialization of 'generate and test' pattern.
How about performance ?
'slow quick and dirty'(S0, S) :-
setof(C, R^(select(C,S0,R),member(C,R)), S).
'better quick and dirty'(S0, S) :-
setof(C, R^(select(C,S0,R),memberchk(C,R)), S).
'still better quick and dirty'(S0, S) :-
setof(H, Done^H^R^(append(Done,[H|R],S0),memberchk(H,R)), S).
test(N) :-
findall(R, (between(1,N,_), random_between(10,100,R)), S),
time('slow quick and dirty'(S, Sa)),
time('better quick and dirty'(S, Sb)),
time('still better quick and dirty'(S, Sc)),
Sa = Sb, Sb = Sc,
time(remove_uniq(S, Sd)),
maplist(length, [Sa, Sb, Sc, Sd], Ls),
writeln(Ls).
25 ?- so:test(100).
% 10,225 inferences, 0.003 CPU in 0.003 seconds (100% CPU, 3506071 Lips)
% 282 inferences, 0.001 CPU in 0.001 seconds (99% CPU, 226231 Lips)
% 254 inferences, 0.001 CPU in 0.001 seconds (100% CPU, 347891 Lips)
% 22,697 inferences, 0.018 CPU in 0.028 seconds (65% CPU, 1272020 Lips)
[28,28,28,28]
true.
26 ?- so:test(1000).
% 1,011,929 inferences, 0.275 CPU in 0.276 seconds (100% CPU, 3674049 Lips)
% 3,015 inferences, 0.013 CPU in 0.013 seconds (98% CPU, 239535 Lips)
% 2,924 inferences, 0.013 CPU in 0.016 seconds (82% CPU, 216598 Lips)
% 351,724 inferences, 0.262 CPU in 0.272 seconds (96% CPU, 1343870 Lips)
[91,91,91,91]
true.
Out of curiosity, I've included remove_uniq/2, but, I think it's not strictly comparable, given the different semantic.
Using member/2 we have a quadratic complexity.
The runtime efficiency of memberchk (implemented as builtin, in C) just squeeze away the filter delay: efficiency become almost linear.
append/3 instead of select/3 allows a further small improvement.