title: Prolog: add list to set
body: <p>The below predicate adds item X to list S.  It works fine.</p>
  
  <pre><code>addToSet(X, S, S) :-
      atomic(X),
      member(X, S),
      !.
  addToSet(X, S, [X|S]) :-
      atomic(X).
  </code></pre>
  
  <p>I am trying to extend it to a predicate which adds list [H|T] to set S.  It works perfect if [H|T] is only 2 items long... i.e. if T is also atomic.</p>
  
  <pre><code>addToSet([], S, S).
  addToSet([H,T], S, S2) :-
      addToSet(H, S, S1),
      addToSet(T, S1, S2).
  </code></pre>
  
  <p>For example, addToSet([5,6],[1,2,3,4],X). works as I desire.  However, addToSet([5,6,7],[1,2,3,4],X). does not work at all.  I am stumped... there is obviously something wrong with the last 2-3 lines of my code, but I cannot figure it out.  Any tips?</p>
  
  <p>Thanks!</p>