Replace an item in a list in Common Lisp?

Paul Wicks picture Paul Wicks · Oct 4, 2008 · Viewed 33.1k times · Source

I have a list of things (I'll call it L), an index(N) and a new thing(NEW). If I want to replace the thing in L at N with NEW, what is the best way to do this? Should I get the sublist up to N and from N to the end of the list and then glue together a new list from the first part, NEW, and the last part using list? Or is there a better way to do this?

Answer

l0st3d picture l0st3d · Oct 4, 2008
(setf (nth N L) NEW)

should do the trick.