Alex Rivera | Logout

Replace an item in a list in Common Lisp?

Asked 2008-10-04T20:08:43.973
27

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?

Edit
Report

2 Answers

8
(setf (nth N L) T)

is the clearest, most succinct, and fastest way, if what you want to do is a "destructive" modification, i.e. actually change the existing list. It does not allocate any new memory.

answered 2008-10-05T15:07:21.240
-1

quickly you can do it with JS on list-replace

answered 2012-02-09T10:10:58.183

Your Answer