我想知道这个函数是否对顶级符号有影响?
(defun test (gallon)
(setq cup '(D D D D))
(pop cup) )
如果答案是肯定的,那是哪一个?
想知道如何修复此功能以防止这种副作用? 谢谢你回答!
答案 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
不修改全局绑定。