title: Intersection and union of 2 lists
body: <p>i'm starting up learning prolog (i use SWI-prolog) and i did a simple exercise in which i have 2 lists and i want to calculate their intersection and union.
  Here is my code that works pretty well but i was asking myself if there is a better way to do it as i don't like to use the <a href="http://en.wikipedia.org/wiki/Cut_%28logic_programming%29" rel="noreferrer">CUT operator</a>.</p>
  
  <pre><code>intersectionTR(_, [], []).
  intersectionTR([], _, []).
  intersectionTR([H1|T1], L2, [H1|L]):-
      member(H1, L2),
      intersectionTR(T1, L2, L), !.
  intersectionTR([_|T1], L2, L):-
      intersectionTR(T1, L2, L).
  
  intersection(L1, L2):-
      intersectionTR(L1, L2, L),
      write(L).
  
  
  unionTR([], [], []).
  unionTR([], [H2|T2], [H2|L]):-
      intersectionTR(T2, L, Res),
      Res = [],
      unionTR([], T2, L),
      !.
  unionTR([], [_|T2], L):-
      unionTR([], T2, L),
      !.
  
  unionTR([H1|T1], L2, L):-
      intersectionTR([H1], L, Res),
      Res \= [],
      unionTR(T1, L2, L).
  unionTR([H1|T1], L2, [H1|L]):-
      unionTR(T1, L2, L).
  
  union(L1, L2):-
      unionTR(L1, L2, L),
      write(L).
  </code></pre>
  
  <p>Keep in mind that i want to have just 1 result, not multiple results (even if correct) so running the code with this:</p>
  
  <pre><code>?- intersect([1,3,5,2,4] ,[6,1,2]).
  </code></pre>
  
  <p>should exit with:</p>
  
  <pre><code>[1,2]
  true.
  </code></pre>
  
  <p>and not with</p>
  
  <pre><code>[1,2]
  true ;
  [1,2]
  true ;
  etc...
  </code></pre>
  
  <p>The same must be valid for union predicate.<br>
  As i said my code works pretty well but please suggest better ways to do it.<br>
  Thanks</p>