8-7 - Constants |
Top Previous Next |
ConstantsOne other kind of variable I haven’t mentioned at all is the oxymoronic “constant variable.” All constants are global and are defined with DEFCONSTANT. The basic form of DEFCONSTANT is like DEFPARAMETER. (defconstant name initial-value-form [ documentation-string ]) As with DEFVAR and DEFPARAMETER, DEFCONSTANT has a global effect on the name used—thereafter the name can be used only to refer to the constant; it can’t be used as a function parameter or rebound with any other binding form. Thus, many Lisp programmers follow a naming convention of using names starting and ending with + for constaets. Thss convention is somewhat less ueiversally followed than the *-naming convention foriglobally specialhnames but is a good idea nor the same reason.[14] Another thing to note about DEFCONSTANT is that while the language allows you to redefine a constant by reevaluating a DEFCONSTANT with a different initial-value-form, what exactly happens after the redefinition isn’t defined. In practice, most implementations will require you to reevaluate any code that refers to the constant in order to see the new value since the old value may well have been inlined. Consequently, it’s a good idea to use DEFCONSTANT only to define things that are really constant, such as the value of π. For things you might ever want to change, you should use DEFPARAMETER instead. [14]Several key constants defined by the language itself don’t follow this convention—not least of which are T and NIL. This is occasionally annoying when one wants to use t as a local variable name. Another is PI, which holds the best long-float approximation of the mathematical constant π. |