XLISP > XLISP Plus  -  Previous | Next

defvar


Type:   -   defined macro (closure)
Source:   -   init.lsp

Syntax

(defvar symbol [init-value])
symbol - an expression evaluating to a symbol
init-value - an optional initial value expression
returns - the value of symbol

Description

The 'defvar' macro defines a user variable with the name 'symbol'. If 'symbol' did not already exist, the 'symbol' is created with the initial value NIL. If the optional 'init-value' expression is present, the new 'symbol' will be set to the 'init-value'. If 'symbol' did exist, its previous value will be left untouched. 'defvar' returns the value of 'symbol' as its result.

(defmacro defvar (sym &optional val)
  `(if (boundp ',sym) ,sym (setq ,sym ,val)))

Note: The 'defvar' macro is intended to simplify porting code from Common Lisp to XLISP. XLISP does not have different concepts of 'variables', 'parameters' and 'constants'. In XLISP they are all variables, so the only difference is that the 'defvar' macro prevents overriding the value of an already existing variable. This macro is not included in the standard Nyquist distribution. If you want to use it, copy the code above to your 'init.lsp' file.

Examples

(boundp 'mvyar)     ; returns NIL - doesn't exist
(defvar myvar)      ; returns NIL - no init-value was given
(boundp 'myvar)     ; returns T
(setq myvar 7)      ; returns 7
(defvar myvar)      ; returns 7 - the variable was not changed
(defvar myvar 99)   ; returns 7 - the variable was not changed

(boundp 'newyar)    ; returns NIL - doesn't exist
(defvar newvar 99)  ; returns 99 - a init-value of 99 was given
newvar              ; returns 99
(defvar newvar)     ; returns 99 - the variable was not changed
(defvar newvar 7)   ; returns 99 - the variable was not changed

Common Lisp: In Common Lisp, the definition of 'defvar' is such that it returns the 'symbol' as its result, XLISP returns the value of 'symbol'. Common Lisp supports a documentation string as an additional optional parameter, XLISP does not support this.

See also the XLISP Plus defconstant and defparameter functions.

  Back to Top


XLISP > XLISP Plus  -  Previous | Next