Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the nth element of a list

I want to change the nth element of a list and return a new list.

I've thought of three rather inelegant solutions:

(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))

What is the best way of doing this?

like image 517
johnsondavies Avatar asked May 19 '11 13:05

johnsondavies


1 Answers

How about

(defun set-nth4 (list n val)
  (loop for i from 0 for j in list collect (if (= i n) val j)))

Perhaps we should note the similarity to substitute and follow its convention:

(defun substitute-nth (val n list)
  (loop for i from 0 for j in list collect (if (= i n) val j)))

BTW, regarding set-nth3, there is a function, constantly, exactly for situation like this:

(defun set-nth3 (list n value)
  (substitute value nil list :test (constantly t) :start n :count 1))

Edit:

Another possibility:

(defun set-nth5 (list n value)
  (fill (copy-seq list) value :start n :end (1+ n)))
like image 184
huaiyuan Avatar answered Sep 28 '22 13:09

huaiyuan