title: Prolog unifying two lists
body: <p>Alright, so I was trying to unify this two lists: <code>[2]</code> and <code>[1,_,3]</code> giving an answer of <code>[1,2,3]</code>. My code is below:</p>
  
  <pre><code>unify([],[],_).
  unify(List1, [Head|Rest], List2) :- member(List1,Head),!,
                                           unify([X|_], Rest, [X|List2]).
  unify(List1, [Head|Rest], [Head|List2]) :- unify(List1, Rest, List2).
  </code></pre>
  
  <p>when i put <code>?-unify([2],[1,_,3],L)</code> it gives me false, but I want it to give <code>L=[1,2,3]</code>. How can I improve the above code?</p>
  
  <p>I tried a trace too but I couldn't figure out.</p>
  
  <pre><code>[debug] 5 ?- unify([2],[1,_,3],L).
  T Call: (6) unify([2], [1, _G512, 3], _G520)
  T Redo: (6) unify([2], [1, _G512, 3], _G520)
  T Call: (7) unify([2], [_G512, 3], _G602)
  T Call: (8) unify([_G607|_G608], [3], [_G607|_G602])
  T Redo: (8) unify([_G607|_G608], [3], [_G607|_G602])
  T Call: (9) unify([3|_G608], [], _G602)
  T Fail: (9) unify([3|_G608], [], _G602)
  T Fail: (8) unify([_G607|_G608], [3], [_G607|_G602])
  T Fail: (7) unify([2], [_G512, 3], _G602)
  T Fail: (6) unify([2], [1, _G512, 3], _G520)
  false
  </code></pre>