title: Prolog: Removing the duplicates of the merged list
body: <p>I am doing a homework need to implement a relation list_merge (X,Y,Z) where X and Y are integer lists, Z contains all elements from both X and Y, and there is no duplicate in Z.</p>
  
  <p>I know how to implement the append function and remove duplicates function.</p>
  
  <p>append</p>
  
  <pre><code>append2([X|A],B,[X|C]):-!,append(A,B,C).
  append2([],B,B).
  </code></pre>
  
  <p>remove duplicates</p>
  
  <pre><code>delete(X,[],[]).
  delete(X,[X|T],R) :- delete(X,T,R).
  delete(X,[H|T],[H|R]) :- not(X = H), delete(X,T,R).
  
  remdups([],[]).
  remdups([H|T],[H|R]) :- delete(H,T,S), remdups(S,R).
  </code></pre>
  
  <p>But I don't know how to combine these two releations in one relation so that I can remove the duplicates after merge the two lists. How can I combine the relations or any other methods which can have the same effect.</p>
  
  <p>Some sample input and output:</p>
  
  <pre><code>?- list_merge([],[],L).
  L = [].
  ?- list_merge([1],[1],L).
  L = [1].
  ?- list_merge([],[1,3,1,1,1],L).
  L = [1, 3] .
  ?- list_merge([5,3,1,2,1],[7,3,2,5,7],L).
  L = [5, 7, 3, 2, 1] .
  </code></pre>