title: Prolog: Removing Duplicates
body: <p>I am trying to remove duplicate entries from a list in prolog. So a list [a,b,a,c,b,a] would return [a,b,c]. I can not use any built in functions. I searched here and found this code.</p>
  
  <pre><code>member(X,[X|_]) :- !.
  member(X,[_|T]) :- member(X,T).
  set([],[]).
  set([H|T],[H|Out]) :- not(member(H,T)), set(T,Out).
  set([H|T],Out) :- member(H,T), set(T,Out).
  </code></pre>
  
  <p>But that would take my list and return [c,b,a] not [a,b,c]</p>
  
  <p>I have remove code that will take an element and a list and return a list with occurrences of that element in the list removed. So I tried to incorporate that into my remove duplicate method but I don't really understand prolog very well so it is not working. Logically I want to take a list cons the head with the recursive call on the new list minus all occurrences of the head. This is what the code would look like in sml. </p>
  
  <pre><code>fun remv(_,nil) = nil
  |   remv(a,x::xs) = if x=a then remv(a,xs) else x::remv(a,xs);
  fun remvdub (nil) = nil
  |   remvdub(x::xs) = x::remvdub(remv(x,xs)); 
  </code></pre>
  
  <p>So this is what I tried in prolog</p>
  
  <pre><code>remv(_,[],[]).
  remv(X,[X|T],Ans) :- remv(X,T,Ans).
  remv(X,[H|T],[H|K]) :- remv(X,T,K). 
  
  remvdub([],[]).
  remvdub([H|T],[H|Ans]) :- remvdub(Ans1,Ans), remv(H,T,Ans1).
  </code></pre>
  
  <p>What am I missing?</p>