name-to-symbol 
Author Message
 name-to-symbol

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.



Sun, 11 Nov 2001 03:00:00 GMT  
 name-to-symbol

Quote:
> 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.

Quote:
> 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?

Quote:
> (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".

Quote:
> (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.


Sun, 11 Nov 2001 03:00:00 GMT  
 name-to-symbol

Quote:


> > 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.

Quote:
> 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



Sun, 11 Nov 2001 03:00:00 GMT  
 name-to-symbol


Quote:


>> > (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.

--

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.



Sun, 11 Nov 2001 03:00:00 GMT  
 name-to-symbol
* Kent M Pitman
| 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.

  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



Mon, 12 Nov 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. procedure names and symbols

2. Storing method names as Symbols

3. How to get symbol-value from symbol-name

4. HELP-how do I convert input symbol into list name

5. HELP-how do I convert input symbol into list name

6. gcc,ELF symbol names

7. symbol names for structs

8. gcc,ELF symbol names

9. Variable names beginning with $ symbol

10. Symbol names under POSIX, f77, and f2c

11. Displaying symbol name and value

12. How to use SYMBOL-NAME?

 

 
Powered by phpBB® Forum Software