当前位置:  开发笔记 > 开发工具 > 正文

Emacs Lisp:如何避免插入重复的列表项?

如何解决《EmacsLisp:如何避免插入重复的列表项?》经验,为你挑选了1个好方法。

如何检查字符串是否已经存在于Emacs Lisp的列表中?我需要检查某个路径字符串是否已经在exec-path中,然后将其添加到该列表中(如果不是).谢谢!



1> cobbal..:

add-to-list函数将在添加之前自动检查

(setq a '(1 2 3))
(add-to-list 'a 4)
(add-to-list 'a 3)

将导致a等于(4 1 2 3)

来自Ch f add-to-list:

add-to-list is a compiled Lisp function in `subr.el'.
(add-to-list list-var element &optional append compare-fn)

Add element to the value of list-var if it isn't there yet.
The test for presence of element is done with `equal',
or with compare-fn if that's non-nil.
If element is added, it is added at the beginning of the list,
unless the optional argument append is non-nil, in which case
element is added at the end.

The return value is the new value of list-var.

If you want to use `add-to-list' on a variable that is not defined
until a certain package is loaded, you should put the call to `add-to-list'
into a hook function that will be run only after loading the package.
`eval-after-load' provides one way to do this.  In some cases
other hooks, such as major mode hooks, can do the job.


这很棒!谢谢!
推荐阅读
TXCWB_523
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有