我有这个列表:
("a" "b" "c" "d" "e")
我想将“d”移动到第一个位置:
("d" "a" "b" "c" "e")
有什么简单的方法可以做到这一点吗?
编辑
感谢您的回答。我研究了一下,然后这样做了:
(defn move-item [data item-to-move]
(conj (remove #(= % item-to-move) data) item-to-move))
(move-item ["a" "b" "c" "d" "e"] ["d"])
我不确定这是否是好的设计,但它确实有效。
最佳答案
可能有用的功能:
1.旋转
user=> (defn rotate [xs] (cons (last xs) (drop-last xs)))
#'user/rotate
user=> (rotate '(1 2 3))
(3 1 2)
2. replace
user=> (replace {1 4} [1 2 3 4])
[4 2 3 4]
关于list - Clojure:移动列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193461/