交换Common Lisp列表中的元素 [英] Swapping elements in a Common Lisp list

查看:151
本文介绍了交换Common Lisp列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在Common Lisp函数,该函数会在给定索引的情况下交换列表中的两个元素并返回修改后的列表?

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

推荐答案

您可以使用 rotatef :

You can use rotatef:

(rotatef (nth i lst) (nth j lst))

当然,列表索引可能会很昂贵(成本为O(列表的大小)),因此,如果您以任何规律性进行此操作,则希望使用数组:

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))

这篇关于交换Common Lisp列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆