elisp - 仅将元素的第一个外观删除到列表中?

标签 elisp

如何仅将元素的第一次出现删除到列表(elisp)中?

最佳答案

您可以使用此 elisp(需要 cl ):

(defun remove-first (elt seq)
  (let ((remove-first t))
    (remove-if (lambda (e) (when (and remove-first (equal elt e))
                             (setq remove-first nil)
                             t))
               seq)))

注意:这会复制原始列表。对于使用副作用的人,请尝试以下操作:
(defun remove-first* (elt seq)
  (if (equal elt (car seq))
      (cdr seq)
    (while (cdr seq)
      (if (equal elt (cadr seq))
          (progn (setcdr seq (cddr seq))
                 (setq seq nil))
        (setq seq (cdr seq))))
    seq))

注意:当第一个元素被移除时,只返回 cdr ,所以像往常一样使用这种类型的操作,像这样调用它:
(setq mylist (remove-first* 3 mylist))

关于elisp - 仅将元素的第一个外观删除到列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4491277/

相关文章:

emacs - 在 emacs lisp 中启动进程,该进程在 lisp 程序结束后继续

function - 如何在 elisp 中的任意点退出函数

ruby - 如何在 elisp 中定义以 "blocks"作为参数的函数?

Emacs:轻松导航图像?

python - Emacs Jedi python 工具无法加载 "error searching for program: permission denied, python"?

emacs - 如何在 emacs lisp 中硬编码参数?

emacs - 如何使用elisp将十六进制字符串转换为ASCII?

emacs - 何时在 Emacs Lisp 中引用符号

emacs - 在 emacs 中运行 emacs

Emacs:如何编写一个作用于区域的defun,但如果没有区域则作用于点?