gpt4 book ai didi

lisp - map 车到位 : destructively modify a list of lists

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:18 25 4
gpt4 key购买 nike

我有一个列表列表:(setq xs (list (list 1 2 3) (list 4 5 6) (list 7 8 9)))。我想从每个列表中删除第一个元素以获取 ((2 3) (5 6) (8 9))。非破坏性地做到这一点很容易:(mapcar 'cdr xs)。但我想改变原始列表。我试过:

(mapcar (lambda (x) (setf x (cdr x))) xs)
(mapcar (lambda (x) (pop x)) xs)

但它不起作用。如何在不创建任何临时列表的情况下尽可能高效地就地更改每个 xs 变量列表?

最佳答案

使用MAP-INTO:

CL-USER 16 > (let ((s (list (list 1 2 3)
(list 4 5 6)
(list 7 8 9))))
(map-into s #'rest s))
((2 3) (5 6) (8 9))

关于lisp - map 车到位 : destructively modify a list of lists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com