Page 1 of 1
What the heck is *unbound* ?
Posted: Fri Nov 22, 2013 2:46 am
by Paul L
I discovered this weird thing in the *obarray*.
Do this:
Nyquist prints
CAR
Unsurprising, right? But now do this:
Nyquist says
error: unbound variable - X
if continued: try evaluating symbol again
Function: #<FSubr-LET: #7cf6238>
Arguments:
((X (QUOTE *UNBOUND*)))
(PRINT X)
1>
Weird!
Re: What the heck is *unbound* ?
Posted: Fri Nov 22, 2013 4:24 am
by steve
*unbound* is a system constant that is equivalent to "no value".
Normally if you use LET, SETQ or SETF, you are binding a symbol (or field) to a value, but with (SETQ x '*UNBOUND*) you are doing the opposite - you are binding to "no value", or to put it another way you are "un-binding" x.
Code: Select all
(setq x 10)
(print x) ; prints "10"
(setq x '*unbound*)
(print x) ; prints "error: unbound variable - X"
Re: What the heck is *unbound* ?
Posted: Fri Nov 22, 2013 4:40 am
by steve
*SCRATCH* is a special variable in that it survives from one run to the next.
This is not a good example of how to use *SCRATCH* but I think it's quite a good example of how '*UNBOUND* works.
Try running this code a few times:
Code: Select all
(print *scratch*)
(if (not (boundp '*scratch*))
(setq *scratch* "*scratch* now has a string value")
(progn
(print *scratch*)
(setq *scratch* '*unbound*)))
Re: What the heck is *unbound* ?
Posted: Fri Nov 22, 2013 4:52 am
by Paul L
The funny thing was, I tried to make a list of all things in *obarray* and iterate it with dolist. But at a certain point, my local loop variable was bound to '*unbound* which was in the list, and the code in the body of the loop made an error because my local variable was unbound!
The workaround was to do (mapcar #'list symbols), and then my loop variable was bound to '(*unbound*) and not '*unbound*, and then I could take the car to get the symbol just like any other.
Common Lisp: the Language says there should be makunbound an fmakunbound functions for unbinding symbols, but as we know XLisp does not implement all of that Common Lisp standard.