我正在使用GCL在Ubuntu上编程。根据来自各种来源的Common Lisp文档,我了解到let
创建本地变量,而setq
设置现有变量的值。 在下面的情况中,我需要创建两个变量并对它们的值求和。
使用setq
(defun add_using_setq ()
(setq a 3) ; a never existed before , but still I'm able to assign value, what is its scope?
(setq b 4) ; b never existed before, but still I'm able to assign value, what is its scope?
(+ a b))
使用 let
(defun add_using_let ( )
(let ((x 3) (y 4)) ; creating variables x and y
(+ x y)))
在这两种情况下,我似乎都能达到相同的结果。在使用setq
和let
之间有什么区别?为什么不能在需要使用let
的所有地方都使用setq
(因为它在语法上更容易)?
setq
在语法上很容易,为什么它会让代码的读者产生疑问,比如“它的作用域是什么?” - Kaz