From: Pascal Bourguignon
Subject: Re: uninterned symbols
Date: 
Message-ID: <87irp9ia36.fsf@thalassa.informatimago.com>
Jeffery Zhang <····@cornell.edu> writes:
> [...first question answered elsewhere in this thread...]
> Another question is what happens when I do (setf (gensym) 1)? 

Nothing good.  See below.


> Does an uninterned symbol represent a variable? 

Yes,  any symbol, even uninterned can name a variable, be it a lexical
variable or a special variable.


> Is it garbage collected immediately or does it just float around and
> become a memory leak?

The problem we have here is that SETF of an undefined variable is
implementation dependant, nose dragons flying here.  

Some implementations try to be helpful and create a new global special
variable, but you shouldn't count on it.

Since you're creating a new symbol, it cannot name an _existing_
variable, So you must define a new special variable, or bind a new
lexical variable:

       (defvar       (gensym) 1)
       (defparameter (gensym) 1)

       (eval `(let ((,(gensym) 1)) ...)) ; or
       (let ((#.(gensym) 1)) ...)

With the above defvar or defparameter, you define a new special
variable with an uninterned name.  With the above let, you define a
new lexical variable, with an uninterned name.

As was explained in the other answers, you then have a problem to
refer to these variables.  You need to keep track of the uninterned
symbol if you want to use it twice or more.


For a global special variable, you could do it like this:

       (defparameter var-name (gensym))
       (eval `(defvar       ,varname 1))
       (eval `(defparameter ,varname 1)) ; or
       (defvar        #.var-name 1)
       (defparameter  #.var-name 1)

       (setf (symbol-value var-name) 2)
       (print #.var-name)

Or:
       (progn 
          (defparameter #1=(gensym) 1)
          (incf #1#)
          (print #1#))

For a lexical variable, you could do it like this:

       (let ((var-name (gensym)))
          (eval `(let ((,var-name 1)) (incf ,var-name)  (print ,var-name)))
          (let ((#.var-name 1))       (incf #.var-name) (print #.var-name)))
       (let ((#1=(gensym) 1))         (incf #1#)        (print #1#))


Note that a macro gets a free "EVAL", so in a macro, you'd just write:

    (defmacro exemple (init-val)
       (let ((var-name (gensym)))
         `(progn
              (defparameter ,var-name ,init-val)
              (incf ,var-name)
              (print ,var-name))))

    (defmacro exemple (init-val)
       (let ((var-name (gensym)))
         `(let ((,var-name ,init-val))
             (incf ,var-name)
             (print ,var-name))))

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Pour moi, la grande question n'a jamais �t�: �Qui suis-je? O� vais-je?� 
comme l'a formul� si adroitement notre ami Pascal, mais plut�t: 
�Comment vais-je m'en tirer?� -- Jean Yanne

From: Marcin 'Qrczak' Kowalczyk
Subject: Re: uninterned symbols
Date: 
Message-ID: <87vet95mb3.fsf@qrnik.zagroda>
Pascal Bourguignon <···@informatimago.com> writes:

>        (defvar       (gensym) 1)
>        (defparameter (gensym) 1)
>
>        (eval `(let ((,(gensym) 1)) ...)) ; or
>        (let ((#.(gensym) 1)) ...)
>
> With the above defvar or defparameter, you define a new special
> variable with an uninterned name.

No: the first argument of defvar or defparameter is not evaluated
as an expression, I believe it must be a literal variable name.

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Pascal Bourguignon
Subject: Re: uninterned symbols
Date: 
Message-ID: <8764l9i7fu.fsf@thalassa.informatimago.com>
Marcin 'Qrczak' Kowalczyk <······@knm.org.pl> writes:

> Pascal Bourguignon <···@informatimago.com> writes:
>
>>        (defvar       (gensym) 1)
>>        (defparameter (gensym) 1)
>>
>>        (eval `(let ((,(gensym) 1)) ...)) ; or
>>        (let ((#.(gensym) 1)) ...)
>>
>> With the above defvar or defparameter, you define a new special
>> variable with an uninterned name.
>
> No: the first argument of defvar or defparameter is not evaluated
> as an expression, I believe it must be a literal variable name.

Oops, true.  What was I thinking?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

IMPORTANT NOTICE TO PURCHASERS: The entire physical universe,
including this product, may one day collapse back into an
infinitesimally small space. Should another universe subsequently
re-emerge, the existence of this product in that universe cannot be
guaranteed.