Is there a Common Lisp function that will swap two elements in a list given their indices and return the modified list?
相关问题
- how to split a list into a given number of sub-lis
- C#: How do i get 2 lists into one 2-tuple list in
- F#: Storing and mapping a list of functions
- Select first row from multiple dataframe and bind
- Generating powerset in one function, no explicit r
相关文章
- List可以存储接口类型的数据吗?
-
C# List
.FindAll 时 空指针异常 - What is the complexity of bisect algorithm?
- Given a list and a bitmask, how do I return the va
- Why does slice [:-0] return empty list in Python
- Does learning one Lisp help in learning the other?
- Style bullet-list with arrows
- bash: /bin/ls: Argument list too long
You can use
rotatef
: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:
I would avoid indexing into the list twice by using
nthcdr
to get the cdr of the cons cell containing the first element that you want to swap and then useelt
to get the remaining element out of the sublist. This means that you only have to actually index starting from the head of the list once.At least from my perspective, this is sufficiently tedious to justify a function.