Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping elements in a Common Lisp list

Is there a Common Lisp function that will swap two elements in a list given their indices and return the modified list?

like image 625
Matthew Piziak Avatar asked Oct 16 '10 20:10

Matthew Piziak


1 Answers

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))
like image 136
Pi Delport Avatar answered Nov 17 '22 12:11

Pi Delport