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

Question on converting string to symbol

534 views
Skip to first unread message

Jaap Weel

unread,
Jun 22, 2000, 3:00:00 AM6/22/00
to
Just a little question. If it's a stupid one, please point to the
appropriate chapter in CLtL2.

I can convert symbols to strings:
(string 'abcd) => "abcd"
I'd like to do the reverse now.
(string-to-symbol "abcd") => ABCD

If you want to know what project i need this for, just look in
alt.fan.hofstadter. It's a very tiny group, so the only message
containing "LISP" in its header is easily found.

Jason Trenouth

unread,
Jun 22, 2000, 3:00:00 AM6/22/00
to
On Thu, 22 Jun 2000 12:02:26 GMT, ajw...@worldonline.nl (Jaap Weel) wrote:

> Just a little question. If it's a stupid one, please point to the
> appropriate chapter in CLtL2.

Start using the actual ANSI Common Lisp standard or a text derived from it like
the HyperSpec (http://www.xanalys.com/software_tools/reference/HyperSpec/)
instead of the old CLtL2 snapshot.

> I can convert symbols to strings:
> (string 'abcd) => "abcd"
> I'd like to do the reverse now.
> (string-to-symbol "abcd") => ABCD

Lookup up INTERN and FIND-SYMBOL in the standard.

__Jason

Alexander Clark

unread,
Jun 22, 2000, 3:00:00 AM6/22/00
to
On Thu, 22 Jun 2000 12:02:26 GMT, ajw...@worldonline.nl (Jaap Weel)
wrote:

>Just a little question. If it's a stupid one, please point to the
>appropriate chapter in CLtL2.
>

>I can convert symbols to strings:
>(string 'abcd) => "abcd"
>I'd like to do the reverse now.
>(string-to-symbol "abcd") => ABCD
>

>If you want to know what project i need this for, just look in
>alt.fan.hofstadter. It's a very tiny group, so the only message
>containing "LISP" in its header is easily found.

Chapter 11, section 7 (intern "abcd") or maybe find-symbol


Erik Naggum

unread,
Jun 22, 2000, 3:00:00 AM6/22/00
to
* Jaap Weel

| Just a little question. If it's a stupid one, please point to the
| appropriate chapter in CLtL2.

As others have pointed out, too: CLtL2 is superseded by the actual
standard, which I prefer to call CLtS, but that's informal at best.

| I can convert symbols to strings:
| (string 'abcd) => "abcd"
| I'd like to do the reverse now.
| (string-to-symbol "abcd") => ABCD

For hysterical raisons, symbol names in Common Lisp are in uppercase
and the Lisp reader is case insensitive, which throws most people's
expectations off. The symbol named "abcd" prints as |abcd|, and the
symbol named "ABCD" prints as abcd if you have set *print-case* to
:downcase, which many do. In any case, reading abcd yields the
symbol named "ABCD". (eq 'abcd (intern "ABCD")) holds for all abcd.

#:Erik
--
If this is not what you expected, please alter your expectations.

Kent M Pitman

unread,
Jun 22, 2000, 3:00:00 AM6/22/00
to
Erik Naggum <er...@naggum.no> writes:

> In any case, reading abcd yields the
> symbol named "ABCD". (eq 'abcd (intern "ABCD")) holds for all abcd.

Especially when *package* is held constant between the time the
above expression is seen by READ and the time execution of the
expression occurs. ;-)

My only point being, since no one else seems to have mentioned it,
that INTERN and FIND-SYMBOL are, in some sense, not inverses but
are one-to-many functions, the which of the many being determined
by dynamic state (specifically, the binding of *package*). There
is no single symbol which is the result of (intern "ABCD").

This also means people must be careful about
(eq x (intern (string x)))
since if x has a package other than the prevailing package, then
(eq 'foo::x (intern (string 'foo::x)))
will yield false.

As a rule, the right answer to "how do I undo (string sym)?" is not
"(intern sym)" but to ask the question "what are you really trying to do?"
The question is almost surely an indication that the person asking the
question is doing something wrong at a higher level. This is not to
say there aren't good uses for FIND-SYMBOL and INTERN, but undoing a
call to STRING is almost never, in my experience, one of them.

Steven M. Haflich

unread,
Jun 22, 2000, 3:00:00 AM6/22/00
to

Alexander Clark wrote:

> Chapter 11, section 7 (intern "abcd") or maybe find-symbol

You should consider make-symbol as well, depending on whether
you want the symbol interned. All these silly Lisp gurus
apparently miss something in their internalization of CL
language abstractions: a symbol is a first-class object
separate from its internment in packages. (:-)

Erik Naggum

unread,
Jun 23, 2000, 3:00:00 AM6/23/00
to
* "Steven M. Haflich" <haf...@pacbell.net>

| You should consider make-symbol as well, depending on whether you
| want the symbol interned. All these silly Lisp gurus apparently
| miss something in their internalization of CL language abstractions:
| a symbol is a first-class object separate from its internment in
| packages. (:-)

Nah, that's the difference between unintern and unimport.

Alexander Clark

unread,
Jun 23, 2000, 3:00:00 AM6/23/00
to
On Thu, 22 Jun 2000 17:01:55 -0700, "Steven M. Haflich"
<haf...@pacbell.net> wrote:

>
>
>Alexander Clark wrote:
>
>> Chapter 11, section 7 (intern "abcd") or maybe find-symbol
>

>You should consider make-symbol as well, depending on whether
>you want the symbol interned. All these silly Lisp gurus
>apparently miss something in their internalization of CL
>language abstractions: a symbol is a first-class object
>separate from its internment in packages. (:-)

What are some uses for uninterned symbols? Hygienic macros?
I'm not very clear on when to use symbols at all. I tend to avoid
them.

Erik Naggum

unread,
Jun 23, 2000, 3:00:00 AM6/23/00
to
* Alexander Clark <a...@aclark.spam.demon.co.uk>

| What are some uses for uninterned symbols?

They're great when you need to refer to symbols captured by some
other means than their symbol name, or used by some other parts of
the system than the Lisp reader. One nice thing about uninterned
symbols is that two of them can have the same name without colliding.

| Hygienic macros?

Macros. (Hygiene is something the Scheme people need.)

| I'm not very clear on when to use symbols at all. I tend to avoid
| them.

Wow. How do you write code without symbols?

Alexander Clark

unread,
Jun 23, 2000, 3:00:00 AM6/23/00
to
On 23 Jun 2000 10:10:58 +0000, Erik Naggum <er...@naggum.no> wrote:

>* Alexander Clark <a...@aclark.spam.demon.co.uk>

>| I'm not very clear on when to use symbols at all. I tend to avoid
>| them.
>
> Wow. How do you write code without symbols?
>
>#:Erik

With difficulty.

I meant, when are symbols the right data structure to use? What are
the considerations that bear on this decision? What do you gain by
using symbols rather than a user defined data structure?

0 new messages