CAD Lisp and variables

A simple test on the command line… Write or paste the yellow stuff and try to understand.

On the command line…What is it?
(defun test ( / foobar) (setq foobar T))DEfine a FUNction “test” that sets a variable “foobar” to T (True). No need to say you have to finish with an enter.
TESTReturn value of last statement (defun).
!foobarPut ! before the name of a variable to get its value…
nilWe have defined the function only, foobar is nil yet.
(test)Run function TEST.
TReturn value of last statement (setq…).
!foobarWhat is value foobar?
nilFoobar is nil because it is in part ( / foobar), saying: Set this to nil after running the function.
!testAnd the function itself?
< #x33 @fffc51876>It survived and is still loaded under this value.
(setq test nil)What happens if we set the function name to nil?
nilExactly, test is nil, test simply doesn’t exist any more. Setting variables to nil simply destroys, kills, variables from memory.
!testDouble check…
nilAnd confirmed.
(defun test ( / ) (setq foobar T))Define function “test” again, but with ( / ) instead of ( / foobar).
TESTReturn value…
(test)Run function TEST.
TReturn value…
!foobarWhat is value foobar?
TNow foobar is not nil because it is not in part ( / ). This can be handy, foobar survived the function and can be used again for something else. However, it can be polluting and annoying and create errors when it is expected to be not existing.
!testAnd the function itself?
< #x33 @fffc51896>Still loaded, newly defined, different value.
(setq test nil)What happens if we set the function name to nil?
nilGone!
!testCheck…
nilYes, gone.
!foobarAnd foobar?
TStill active 🙂


Posted in CAD

Leave a Reply

Your email address will not be published. Required fields are marked *