这个功能对顶级有影响吗?

时间:2014-06-17 14:18:45

标签: clisp

我想知道这个函数是否对顶级符号有影响?

(defun test (gallon)
  (setq cup '(D D D D))
  (pop cup) )

如果答案是肯定的,那是哪一个?

想知道如何修复此功能以防止这种副作用? 谢谢你回答!

1 个答案:

答案 0 :(得分:1)

如果编译此函数,您将收到警告:

WARNING: in TEST : CUP is neither declared nor bound,
         it will be treated as if it were declared SPECIAL.
WARNING: in TEST : CUP is neither declared nor bound,
         it will be treated as if it were declared SPECIAL.
WARNING: in TEST : CUP is neither declared nor bound,
         it will be treated as if it were declared SPECIAL.
WARNING: in TEST : CUP is neither declared nor bound,
         it will be treated as if it were declared SPECIAL.
WARNING: in TEST : variable GALLON is not used.
         Misspelled or missing IGNORE declaration?

这告诉您,您没有绑定cup而没有使用gallon

正在发生的是您正在创建(和修改)全局变量cup

为了避免它,你应该在本地绑定它:

(defun test1 (gallon)
  (let ((cup '(D D D D)))
    (pop cup)))

注意您仍然忽略gallon参数。

要查看差异,请观察

cup
==> undefined variable error
(test)
==> D
cup
==> (D D D)
(setq cup 10)
==> 10
(test1)
==> D
cup 
==> 10

即,我的test1 修改全局绑定。