3.3. Symbols |
The evaluation of a symbol returns the ``value'' of the symbol. Function setq changes the value of a symbol. The value of a new symbol is always the empty list.
During a call to a Lush function, or during the execution of certain special functions (e.g. function let ) predefined symbols take a temporary value. The previous values are then restored when the function exits.
For instance, assume that we define a function (de sqr(x) (* x x)) . Evaluating list (sqr 4) will perform the following actions:
This dynamical binding makes the interpreter getting faster, but somewhat precludes the development of efficient compilers.
The textual representation of a symbol is composed by the characters of its name. The reader usually converts symbol names to lowercase and replaces underscores by dashes. This can be prevented by quoting the symbol name with vertical bars. Such vertical bars are useful for defining symbols whose name contains any character usually forbidden: parenthesis, spaces, semi-colon, macro-characters, upper-case characters etc....
Examples :
gasp GasP ; are the same symbol named "gasp" |GasP| ; is different symbol named "GasP" |);( '| ; is a symbol named ");( '" "gasp" ; is a string 12 ; is a number |12| ; is a symbol named "12"
Unlike several dialect of lisp, Lush does not provide other fields in a symbol for storing a specific functional value or a property list. The value of a symbol is the only storage provided by symbols.
3.3.0. (defvar name [val]) |
[DM] (sysenv.lsh) |
Traditionally function setq was used for that purpose in SN/TLisp because there was no notion of undefined global variables. All undefined global variables were assumed to contain an empty list. This behavior was the source of many bugs. The lush kernel now prints a warning when using setq to define a new global variable.
3.3.1. (defparameter name [val]) |
[DM] (sysenv.lsh) |
3.3.2. (setq v1 a1 ... [vn an]) |
[DX] |
The setq function has a special behavior when the vi are not symbols. This behavior is documented later, with the scope function.
Example:
? (setq a 3) = 3 ? (setq b 6) = 6 ? a b = 3 = 6 ? (setq a b b a) = 3 ? a b = 6 = 3
3.3.3. (set v a) |
[DX] |
Example:
? (setq s 'a) = a ? (set s 3) = 3 ? s = a ? a = 3
3.3.4. (incr v [n]) |
[DX] |
Example:
? (setq s 0) = 0 ? (incr s) = 1 ? s = 1
3.3.5. (named s) |
[DX] |
Example:
? (named "a") = a
? (named "A") = |A|
3.3.6. (nameof s) |
[DX] |
Example:
? (nameof 'a) = "a"
? (nameof '|A|) = "A"
3.3.7. (namedclean s) |
[DX] |
Example:
? (namedclean "a") = a
? (namedclean "A") = a
? (namedclean "|A|") = |A|
3.3.8. (lock-symbol s1 ... sn) |
[DX] |
3.3.9. (unlock-symbol s1 ... sn) |
[DX] |
3.3.10. (symblist) |
[DX] |
Example:
? (length (symblist)) = 3006
3.3.11. (oblist) |
[DX] |
Example:
? (length (oblist)) = 3006
3.3.12. (macrochp s) |
[DX] |
Example:
? (macrochp '|'|) = t
3.3.13. (putp anything name value) |
[DE] (sysenv.lsh) |
3.3.14. (getp anything name) |
[DE] (sysenv.lsh) |
3.4. Namespaces and Packages |
3.4.0. (private ..symbols..) |
[DM] (sysenv.lsh) |
3.4.1. (package name ..expressions..) |
[DM] (sysenv.lsh) |
Example:
(package foobar (private foo) (de foo(x) (print x)) (de bar(x) (foo x)) )
defines a global symbol bar . Symbol foo however is renamed foobar.foo in all expressions occuring after (private foo) . Hence the above expression would be equivalent to:
(de bar (x) (toto.foo x) ) (de toto.foo (x) (print x) )
Internals: Symbol name is used to identify the group of functions. Function package maintains a list of hidden symbols declared with function private . Each expression is first transformed to replace all occurences of a hidden symbol by a new symbol whose name is composed by prepending the package name name , then evaluated as usual.