What the heck is *unbound* ?

I discovered this weird thing in the obarray.

Do this:

(let ((x 'car))
(print x))

Nyquist prints


Unsurprising, right? But now do this:

(let ((x '*unbound*))
(print x))

Nyquist says

error: unbound variable - X
if continued: try evaluating symbol again
Function: #<FSubr-LET: #7cf6238>


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.

(setq x 10)
(print x) ; prints "10"
(setq x '*unbound*)
(print x) ; prints "error: unbound variable - X"

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:

(print *scratch*)
(if (not (boundp '*scratch*))
    (setq *scratch* "*scratch* now has a string value")
      (print *scratch*)
      (setq *scratch* '*unbound*)))

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.