Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Emacs 将元素以破坏性方式添加到列表的惯用方法?_Emacs_Elisp - Fatal编程技术网

Emacs 将元素以破坏性方式添加到列表的惯用方法?

Emacs 将元素以破坏性方式添加到列表的惯用方法?,emacs,elisp,Emacs,Elisp,有没有一种好方法可以将元素添加到存储在变量中的列表中?我用的方法不是很漂亮 起始列表: (setq sml/hidden-modes (list " hl-p")) 将项目添加到sml/隐藏模式: ;; First way I append the items to hidden-modes and set it again (setq sml/hidden-modes (append sml/hidden-modes (list

有没有一种好方法可以将元素添加到存储在变量中的列表中?我用的方法不是很漂亮

起始列表:

(setq sml/hidden-modes (list " hl-p"))
将项目添加到sml/隐藏模式:

;; First way I append the items to hidden-modes and set it again
(setq sml/hidden-modes (append sml/hidden-modes
                               (list " AC" " Undo-Tree" " Smrt")))

;; Second way I use add-to-list to add items one at a time instead of all at once
(mapcar (lambda (mode) (add-to-list 'sml/hidden-modes mode))
        (list " AC" " Undo-Tree" " Smrt"))

;; Way I see people doing it in random .emacs snippets I find
(add-to-list 'sml/hidden-modes " AC")
(add-to-list 'sml/hidden-modes " Undo-Tree")
(add-to-list 'sml/hidden-modes " Smrt")

如果您事先知道原始列表为非零,可以尝试
ncoc

ELISP> (setq a '(1 2 3))
(1 2 3)

ELISP> (nconc a '(4 5 6))
(1 2 3 4 5 6)

ELISP> a
(1 2 3 4 5 6)

如果您事先知道原始列表为非零,可以尝试
ncoc

ELISP> (setq a '(1 2 3))
(1 2 3)

ELISP> (nconc a '(4 5 6))
(1 2 3 4 5 6)

ELISP> a
(1 2 3 4 5 6)
列表库必须在适当位置预先添加元素:

(let ((xs '(2 3))) (!cons 1 xs) xs) ; (1 2 3)
除了阅读的部分之外。

列表库必须在适当的位置预先添加元素:

(let ((xs '(2 3))) (!cons 1 xs) xs) ; (1 2 3)

除了阅读的部分。

实际上,
(setq sml/隐藏模式(追加sml/隐藏模式(列表…))
不会破坏性地修改该变量
append
复制除最后一个参数外的每个参数。请注意,
addtolist
方法的好处是,如果列表中已经存在元素,则不修改列表。这种幂等性在
.emacs
中很好,因为它意味着配置代码可以无需担心地重新计算。实际上,
(setq sml/隐藏模式(append sml/隐藏模式(list…))
不会破坏性地修改该变量
append
复制除最后一个参数外的每个参数。请注意,
addtolist
方法的好处是,如果列表中已经存在元素,则不修改列表。这种幂等性在
.emacs
中很好,因为它意味着配置代码可以无需担心地重新计算。