【问题标题】:How to use add-to-list on a list returned by function如何在函数返回的列表上使用添加到列表
【发布时间】:2016-01-29 09:40:55
【问题描述】:

我有一个返回列表的函数。它可能会返回一个空白列表或一个数字列表。我想将add-to-list 应用于返回值。有可能吗?

     (defun return-list () body....)
     (setq test (add-to-list (return-list) 1) )

【问题讨论】:

    标签: list elisp


    【解决方案1】:

    函数add-to-list 作用于变量,而不是列表。 例如:

    (defvar test (return-list))
    (add-to-list 'test 1)
    

    如果您要无条件地添加到列表中,请使用宏push 对位置进行操作:

    (push 1 test)
    

    但是,在您的情况下,您可以做得更简单:

    (setq test (cons 1 (return-list)))
    

    如果您只想在元素不存在时添加它,请使用宏 cl-pushnew,它也可以在位置上操作:

    (pushnew 1 test)
    ;; `test' is now (1)
    (pushnew 1 test)
    ;; `test' is still (1)
    

    【讨论】:

    • 确实,我们建议使用cl-pushnew(来自cl-lib)而不是pushnew(来自旧的cl 库)。但重点确实是add-to-list 仅适用于对自定义变量等进行操作时使用。
    猜你喜欢
    • 2019-08-29
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多