title: Cloning,Copying prolog list
body: <p>I found this piece of code everywhere for <strong>copying a list or cloning</strong> a list.</p>
  
  <p><strong><em>Code found everywhere:</em></strong></p>
  
  <pre><code>clone([],[]).
  clone([H|T],[H|Z]):- clone(T,Z).
  
  ?-clone([1,2,3],Z).
    Z=[1,2,3]
  
  ?-clone(2,Z).
    false
  </code></pre>
  
  <p>This doesn't copy anything other than <code>lists</code>.The time complexity of the above code is <code>O(n)</code>.</p>
  
  <p>But Prolog tries to unify right and lift side, right? This can be written in a much simpler way, right?</p>
  
  <p>Like <code>clone1(Z,Z).</code>:</p>
  
  <pre><code>clone1(Z,Z).
  
  ?-clone1([1,2,3],Z).
    Z=[1,2,3]
  ?-clone1(1,Z).
    Z=1
  ?-clone1(!,Z).
    Z =!
  ?-clone1(@,Z).
    Z=(@)
  </code></pre>
  
  <p>I feel <code>clone1(X, X).</code> is much more generic and cloned almost everything passed to it. It did not  clone <code>%</code> ,<code>(</code> , <code>)</code> , <code>()</code>. <code>clone1(%,Z)</code> failed with the message <code>% - used for commenting</code>.The time complexity of <code>clone1</code> is <code>O(1)</code> I may be wrong. In every aspect, <code>clone1</code> is much better than <code>clone</code>.</p>
  
  <p>Why is this clone/copy written not like this, i.e. <code>clone(X, X).</code> What am I missing? Please explain to me the difference between both codes I provided above. If both do the same why is <code>clone1(X, X).</code> not used and no one had posted about it.</p>