Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

name-to-symbol

8 views
Skip to first unread message

joachim de beule

unread,
May 26, 1999, 3:00:00 AM5/26/99
to
Hello,

I was wondering if there is a way to get a bound symbol
from a given name-string (knowing in advance this symbol is
interned somewhere else.) This would be like some
inverse 'symbol-name' function.

(setf test 5)
(symbol-name (name-to-symbol "test"))
=> "test"
(symbol-value (name-to-symbol "test"))
=>5

(I realize one has problems when several symbols have the same name
but who ever needs that ?)
--
Joachim De Beule,
Institute for Nuclear Physics,
RUG - Belgium.

Kent M Pitman

unread,
May 26, 1999, 3:00:00 AM5/26/99
to
joachim de beule <joa...@inwpent3.rug.ac.be> writes:

> I was wondering if there is a way to get a bound symbol

I don't think you mean "bound" here in the CLHS glossary sense of "bound".
Symbols are either bound or they are not, but that is an orthogonal property
to whether you can look them up by name.

> from a given name-string (knowing in advance this symbol is
> interned somewhere else.) This would be like some
> inverse 'symbol-name' function.

Well, symbol-name is many-to-one, so of course you must specify a package.
Any reason INTERN doesn't work?



> (setf test 5)
> (symbol-name (name-to-symbol "test"))
> => "test"
> (symbol-value (name-to-symbol "test"))
> =>5

By the way, the symbol-name of the test symbol you have assigned above
is not "test" but "TEST". You have to do
(setf |test| 5)
to change the value of the symbol named "test". By default, the reader
upcases symbol names before calling intern.
(setf test 5)
is the same as
(SETF TEST 5)
and the internal name of the symbols are "SETF" and "TEST".



> (I realize one has problems when several symbols have the same name
> but who ever needs that ?)

Most programmers need this most of the time. However, INTERN will
default its second argument to the value of *PACKAGE*, leaving you
with it working on the current package, and correctly supporting the
illusion that you have the only package in the universe if that's
the illusion you want.

David A. Duff

unread,
May 26, 1999, 3:00:00 AM5/26/99
to
In article <sfw3e0j...@world.std.com>, Kent M Pitman
<pit...@world.std.com> wrote:

> joachim de beule <joa...@inwpent3.rug.ac.be> writes:
>
> > I was wondering if there is a way to get a bound symbol
>
> I don't think you mean "bound" here in the CLHS glossary sense of "bound".
> Symbols are either bound or they are not, but that is an orthogonal property
> to whether you can look them up by name.
>
> > from a given name-string (knowing in advance this symbol is
> > interned somewhere else.) This would be like some
> > inverse 'symbol-name' function.
>
> Well, symbol-name is many-to-one, so of course you must specify a package.
> Any reason INTERN doesn't work?
>
> > (setf test 5)
> > (symbol-name (name-to-symbol "test"))
> > => "test"
> > (symbol-value (name-to-symbol "test"))
> > =>5
>
> By the way, the symbol-name of the test symbol you have assigned above
> is not "test" but "TEST".

this is only true if we assume name-to-symbol calls #'read or some variant
(i.e., something which processes tokens according to the value of
*read-case*). it is quite plausible to imagine a version of
name-to-symbol that would not do that.

> You have to do
> (setf |test| 5)
> to change the value of the symbol named "test". By default, the reader
> upcases symbol names before calling intern.
> (setf test 5)
> is the same as
> (SETF TEST 5)
> and the internal name of the symbols are "SETF" and "TEST".

what you say of course is true, but here you are talking about the lisp
reader, whereas above, some arbitrary function that processes strings was
being described and there's no reason whatsoever to think that such a
function would or should follow the conventions of the reader.

--
David Duff
IET
du...@iet.com

Barry Margolin

unread,
May 26, 1999, 3:00:00 AM5/26/99
to
In article <duff-26059...@luperfoy.100.168.192.in-addr.arpa>,

David A. Duff <du...@iet.com> wrote:
>In article <sfw3e0j...@world.std.com>, Kent M Pitman
><pit...@world.std.com> wrote:
>> > (setf test 5)
>> > (symbol-name (name-to-symbol "test"))
>> > => "test"
>> > (symbol-value (name-to-symbol "test"))
>> > =>5
>>
>> By the way, the symbol-name of the test symbol you have assigned above
>> is not "test" but "TEST".
>
>this is only true if we assume name-to-symbol calls #'read or some variant
>(i.e., something which processes tokens according to the value of
>*read-case*). it is quite plausible to imagine a version of
>name-to-symbol that would not do that.

The symbol was not assigned in the call to name-to-symbol. Kent was
referring to the symbol assigned in the (setf test 5) expression. His
point was that you should use (name-to-symbol "TEST") to get that symbol,
since the Common Lisp reader uppercases symbols by default.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Erik Naggum

unread,
May 27, 1999, 3:00:00 AM5/27/99
to
* Kent M Pitman

| By the way, the symbol-name of the test symbol you have assigned above is
| not "test" but "TEST".

* du...@iet.com (David A. Duff)


| this is only true if we assume name-to-symbol calls #'read or some
| variant (i.e., something which processes tokens according to the value of
| *read-case*). it is quite plausible to imagine a version of
| name-to-symbol that would not do that.

if you are talking about Common Lisp, I know Kent was, there is no such
thing as a *READ-CASE* variable that controls the case. there is a
READTABLE-CASE accessor that takes a readtable, the current of which is
the value of *READTABLE*.

| what you say of course is true, but here you are talking about the lisp
| reader, whereas above, some arbitrary function that processes strings was
| being described and there's no reason whatsoever to think that such a
| function would or should follow the conventions of the reader.

there is no reason whatsoever that such a function should _not_ follow
the conventions of reader. there is an ample supply of reasons that it
should, such as the principle of least surprise, or the stronger version
of same: a prohibition against forcing the willfully confusing on users.

unless people use a Lisp that is not a Common Lisp, the case of the name
of symbols that read and print without special syntax is upper-case.
programmers should be aware of this, should be able to expect this, and
should not do anything special to cater to different conventions unless
they want to be portable to a Lisp that is not a Common Lisp.

however, it should come as no surprise that I prefer to read lowercase
instead of uppercase in code. (in prose, I prefer to let symbols stand
out, and uppercase is very convenient for this purpose.) this is where
the variable *PRINT-CASE* comes in. the only problem is that one needs
to type in uppercase when dealing with names of symbols. since that is a
context worth noting in its own right, I use a special read macro to help
me, and Allegro CL has a convenient internal function that returns the
casified string. I have bound it to #", so a symbol name reads #"foo",
and the reader returns what the symbol-name of the symbol whose string
representation were "foo" would have been, without interning the symbol
-- or, in simpler terms, it returns the string that would be given to
INTERN to return the symbol. I want it to be pervasive, so I have the
following code in the customization files when building new images:

#+franz-inc excl:
(loop
with readtables = (get-objects 11)
for i from 1 to (aref readtables 0)
for *readtable* = (aref readtables i)
when (readtable-dispatch-tables *readtable*) do
;; reader for symbol names that does case conversion according to the
;; rest of the symbol reader. thanks to Sean Foderaro for the pointer.
(set-dispatch-macro-character #\# #\"
(named-function symbol-namestring-reader
(lambda (stream character prefix)
(declare (ignore prefix))
(prog1 (read-extended-token stream)
(unless (char= character (read-char stream))
(internal-reader-error stream "invalid symbol-namestring syntax")))))))

note that it also works with escaped characters: #"|foo|" => "foo"
regardless of case conversion, so there is no limitation to its use,
other than what makes sense stylistically.

(I demand no more than to be credited if you use it and that you identify
any changes you make as your own.)

#:Erik
--
@1999-07-22T00:37:33Z -- pi billion seconds since the turn of the century

0 new messages