Подтвердить что ты не робот

Emacs Lisp: как избежать вставки дублированного элемента списка?

Как проверить, есть ли строка в списке в Emacs Lisp? Мне нужно проверить, есть ли определенная строка пути уже в exec-path, а затем добавить ее в этот список, если это не так. Спасибо!

4b9b3361

Ответ 1

Функция add-to-list будет автоматически проверяться перед добавлением

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

приведет к a, равному (4 1 2 3)

из C-h 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 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.