Is there a Common Lisp function that will swap two elements in a list given their indices and return the modified list?
You can use rotatef
:
(rotatef (nth i lst) (nth j lst))
Of course, list indexing can be expensive (costing O(size of list)), so if you do this with any regularity, you'd rather want to use an array:
(rotatef (aref arr i) (aref arr j))