title: Changing the nth element of a list
body: <p>I want to change the nth element of a list and return a new list.</p>
  
  <p>I've thought of three rather inelegant solutions:</p>
  
  <pre><code>(defun set-nth1 (list n value)
    (let ((list2 (copy-seq list)))
      (setf (elt list2 n) value)
      list2))
  
  (defun set-nth2 (list n value)
    (concatenate 'list (subseq list 0 n) (list value) (subseq list (1+ n))))
  
  (defun set-nth3 (list n value)
    (substitute value nil list 
      :test #'(lambda (a b) (declare (ignore a b)) t)
      :start n    
      :count 1))
  </code></pre>
  
  <p>What is the best way of doing this?</p>