【问题标题】:setting a variable in a function in eLisp在 eLisp 中的函数中设置变量
【发布时间】:2016-04-09 03:40:39
【问题描述】:

试图将函数和列表传递给函数。尝试将列表中的每个项目与以下项目进行比较。

功能是:

(defun my-list-function(fn L)
    (if (< (length L) 2)
        (message "List needs to be longer than 2")
        (progn (setq newL L) ;; Save the list locally
               (while (> (length newL) 1)  ;; While list has 2 items
                      (setq t (car newL)) ;; Get first item
                      (setq newL (cdr newL)) ;; resave list minus first item
                      (funcall #'fn t #'car newL)))))  ;; pas first two items to a function

我不断收到错误 - 设置常量-t

【问题讨论】:

  • 当你想在一个函数中拥有一个局部变量时,你通常在使用它之前将它与let绑定。在这个代码示例中,newL 实际上变成了一个全局变量。

标签: function lisp elisp


【解决方案1】:

t 是保留名称(请参阅11.2 Variables that never change)。使用不同的变量名代替 t 来说明其包含/含义(例如 firstItem)。

【讨论】:

    【解决方案2】:
    (setq newL L) ;; Save the list locally
    

    这不会本地保存newL 不是局部变量。 setq 不声明局部变量。 setq 将变量设置为某个值。

    【讨论】:

    • 投了赞成票,尽管您也应该提到解决方案 :)
    猜你喜欢
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多