all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* comparing symbols coming from gensym?
@ 2005-08-24  4:50 Joe Corneli
  2005-08-24 15:48 ` Drew Adams
  0 siblings, 1 reply; 5+ messages in thread
From: Joe Corneli @ 2005-08-24  4:50 UTC (permalink / raw)



Why is it that

(setq a (gensym)) ;=> G2007
a                 ;=> G2007
(equal a 'G2007)  ;=> nil

while 

(setq a 'foo)    ;=> foo
a                ;=> foo
(equal a 'foo)   ;=> t

?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: comparing symbols coming from gensym?
       [not found] <mailman.4802.1124860720.20277.help-gnu-emacs@gnu.org>
@ 2005-08-24  8:24 ` Alan Mackenzie
  2005-08-24 15:57   ` Joe Corneli
  2005-08-24  9:00 ` N. Raghavendra
  1 sibling, 1 reply; 5+ messages in thread
From: Alan Mackenzie @ 2005-08-24  8:24 UTC (permalink / raw)


Joe Corneli <jcorneli@math.utexas.edu> wrote on Tue, 23 Aug 2005 23:50:46
-0500:

> Why is it that

> (setq a (gensym)) ;=> G2007
> a                 ;=> G2007
> (equal a 'G2007)  ;=> nil

> while 

> (setq a 'foo)    ;=> foo
> a                ;=> foo
> (equal a 'foo)   ;=> t

> ?

This is because your gensymmed symbol, 'G2007, has only been
"half-created".  It hasn't been "interned" in the "obarray", the
structure which holds (almost) all symbols - that 'G2007 is a
free-floating symbol.

When you do (equal a 'G2007), this _creates_ a full-bloodied
industrial-strength NEW _interned_ symbol G2007, since there wasn't
already such a symbol in the obarray.  This is distinct from your
gensymmed one.

That is, in fact, the whole point of gensym - to create a symbol distinct
from any other symbol which ever has been or ever will be.  You asked for
it, you got it!  If you want to actually _use_ it, you'll need something
like (symbol-value a)

Look up `gensym' in the CL manual, and `intern' in the Elisp manual.

-- 
Alan Mackenzie (Munich, Germany)

Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: comparing symbols coming from gensym?
       [not found] <mailman.4802.1124860720.20277.help-gnu-emacs@gnu.org>
  2005-08-24  8:24 ` Alan Mackenzie
@ 2005-08-24  9:00 ` N. Raghavendra
  1 sibling, 0 replies; 5+ messages in thread
From: N. Raghavendra @ 2005-08-24  9:00 UTC (permalink / raw)


At 2005-08-23T23:50:46-05:00, Joe Corneli wrote:

> Why is it that
> 
> (setq a (gensym)) ;=> G2007
> a                 ;=> G2007
> (equal a 'G2007)  ;=> nil

Perhaps this is because `a' is an uninterned symbol --- see
`(elisp)Creating Symbols' --- while `G2007' is an interned symbol.
(`gensym' creates an uninterned symbol.)

ELISP> (intern-soft a)
nil
ELISP> (intern-soft 'G55817)
G55817

> while 
> 
> (setq a 'foo)    ;=> foo
> a                ;=> foo
> (equal a 'foo)   ;=> t

In this case, both `a' and `foo' are interned.

ELISP> (intern-soft a)
foo
ELISP> (intern-soft 'foo)
foo

Raghavendra.

-- 
N. Raghavendra <raghu@mri.ernet.in> | See message headers for contact
Harish-Chandra Research Institute   | and OpenPGP details.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: comparing symbols coming from gensym?
  2005-08-24  4:50 comparing symbols coming from gensym? Joe Corneli
@ 2005-08-24 15:48 ` Drew Adams
  0 siblings, 0 replies; 5+ messages in thread
From: Drew Adams @ 2005-08-24 15:48 UTC (permalink / raw)


    Why is it that
    (setq a (gensym)) ;=> G2007
    a                 ;=> G2007
    (equal a 'G2007)  ;=> nil
    while
    (setq a 'foo)    ;=> foo
    a                ;=> foo
    (equal a 'foo)   ;=> t

Perhaps because gensym, like make-symbol, doesn't intern the symbol it
creates? The reader interns `foo' in (setq a 'foo). So, they are different
symbols: one is interned in obarray; the other is an uninterned symbol.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: comparing symbols coming from gensym?
  2005-08-24  8:24 ` Alan Mackenzie
@ 2005-08-24 15:57   ` Joe Corneli
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Corneli @ 2005-08-24 15:57 UTC (permalink / raw)



   When you do (equal a 'G2007), this _creates_ a full-bloodied
   industrial-strength NEW _interned_ symbol G2007, since there wasn't
   already such a symbol in the obarray.  This is distinct from your
   gensymmed one.

   That is, in fact, the whole point of gensym - to create a symbol distinct
   from any other symbol which ever has been or ever will be.  You asked for
   it, you got it!  If you want to actually _use_ it, you'll need something
   like (symbol-value a)

(setq a (gensym)) ;=> G2016 this morning
(symbol-name a)   ;=> "G2016"
(symbol-value a)  ;=> `void-variable' error

So I guess `symbol-name' is the thing to use.

Incidentally,

(symbol-value 'G2016)

also gives a `void-variable' error.

The moral of the story here is that I shouldn't be using `gensym' but
rather something "gensym-like", better tailored to the situation I'm
working in.


Just to pick up the original thread briefly, the fact that I was using
`equal' and not `eq' is maybe an argument for the comparison to return
`t' even though the two variables are distinct and stored in
different places; remember, it says

,----
| Return t if two Lisp objects have similar structure and contents.
| They must have the same data type.
`----

They have no contents (as evidenced above) and appear to be internally
similar?

(type-of 'G2016)
(type-of a)

Both are symbols.  I guess the interpreter judges them by their
situation in the world as well as their internal contents, which
doesn't seem very egalitarian of it.

Or perhaps the idea is like with infinities, even though both values
are singular, they aren't necessarily equal?

Perhaps some additional information should appear on the `equal'
docstring?

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-08-24 15:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-24  4:50 comparing symbols coming from gensym? Joe Corneli
2005-08-24 15:48 ` Drew Adams
     [not found] <mailman.4802.1124860720.20277.help-gnu-emacs@gnu.org>
2005-08-24  8:24 ` Alan Mackenzie
2005-08-24 15:57   ` Joe Corneli
2005-08-24  9:00 ` N. Raghavendra

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.