Hello,
I just began to learn Common Lisp, and as you can imagine I've got some
questions. What is the difference between defvar and defconstant ?
Both defines a name/value that cannot be changed... Am I wrong ?
Thanks a lot for your help (and sorry for this stupid (I guess)
question).
TomL wrote:
> Hello,
>
> I just began to learn Common Lisp, and as you can imagine I've got some
> questions. What is the difference between defvar and defconstant ?
>
> Both defines a name/value that cannot be changed... Am I wrong ?
No, the value of the DEFVAR-ed symbol may be changed (either by
setting or binding it).
Paul
Thanks for your answer.
Correct me if I'm wrong:
- A symbol defined by DEFVAR can be changed, by a SETQ for instance,
but cannot be changed by another DEFVAR.
- A symbol defined by DEFCONSTANT cannot be changed by any method.
Is that right ?
In article <·······················@f14g2000cwb.googlegroups.com>,
"TomL" <········@vtek.chalmers.se> wrote:
> Thanks for your answer.
>
> Correct me if I'm wrong:
>
> - A symbol defined by DEFVAR can be changed, by a SETQ for instance,
> but cannot be changed by another DEFVAR.
> - A symbol defined by DEFCONSTANT cannot be changed by any method.
>
> Is that right ?
That's basically right. See also DEFPARAMETER .
TomL wrote:
> Hello,
>
> I just began to learn Common Lisp, and as you can imagine I've got some
> questions. What is the difference between defvar and defconstant ?
>
> Both defines a name/value that cannot be changed... Am I wrong ?
>
Kind of, the REPL is your friend, when in doubt try a little
experiment (find a compliant CL).
CL-USER 1 > (defconstant x 1)
X
CL-USER 2 > x
1
CL-USER 3 > (defun xval () x)
XVAL
CL-USER 4 > (xval)
1
CL-USER 5 > (defconstant x 2)
X
CL-USER 6 > x
2
CL-USER 7 > (xval)
2
CL-USER 8 > (compile 'xval)
XVAL
NIL
NIL
CL-USER 9 > (defconstant x 3)
X
CL-USER 10 > (xval)
2
CL-USER 11 > x
3
CL-USER 12 > (macroexpand '(defconstant x 2))
(COMPILER-LET ((DSPEC::*LOCATION* (QUOTE (:INSIDE (DEFCONSTANT X) :LISTENER))))
(COMPILER::TOP-LEVEL-FORM-NAME (DEFCONSTANT X) (EVAL-WHEN (:COMPILE-TOPLEVEL)
(COMPILER::ADD-CONSTANT (QUOTE X) 2 NIL)) (DSPEC:INSTALL-DEFCONSTANT (QUOTE X)
(DSPEC:LOCATION) 2)))
T
CL-USER 13 > (defvar y 1)
Y
CL-USER 14 > (defun yval () y)
YVAL
CL-USER 15 > (compile 'yval)
YVAL
NIL
NIL
CL-USER 16 > y
1
CL-USER 17 > (setf y 2)
2
CL-USER 18 > y
2
CL-USER 19 > (yval)
2
CL-USER 20 >