From: Ulrich Hobelmann
Subject: String->symbol in Lisp?
Date: 
Message-ID: <44mcecF2rvnbU1@individual.net>
I'd like to compare some strings in a case statement for convenience, 
but I don't know how to convert strings into symbols that are "normal".

(eq (intern "foo") 'foo) gives me NIL, so it doesn't work.  The CLHS 
also doesn't really help me.  Make-symbol seems to be something else too.

Does Lisp have something like Scheme's string->symbol?

-- 
Suffering from Gates-induced brain leakage...

From: Peter Seibel
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <m2oe1lanvu.fsf@gigamonkeys.com>
Ulrich Hobelmann <···········@web.de> writes:

> I'd like to compare some strings in a case statement for convenience,
> but I don't know how to convert strings into symbols that are
> "normal".
>
> (eq (intern "foo") 'foo) gives me NIL, so it doesn't work.  The CLHS
> also doesn't really help me.  Make-symbol seems to be something else
> too.
>
> Does Lisp have something like Scheme's string->symbol?

INTERN is the right thing however you need to be aware of case. Since
the Lisp reader normally upcases all symbols, when you read 'foo you
get a symbol whose name is "FOO" while when you evaluate (intern
"foo") you get a symbol whose name is "foo".

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: Ulrich Hobelmann
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <44mmn8F2tl99U1@individual.net>
Peter Seibel wrote:
>> Does Lisp have something like Scheme's string->symbol?
> 
> INTERN is the right thing however you need to be aware of case. Since
> the Lisp reader normally upcases all symbols, when you read 'foo you
> get a symbol whose name is "FOO" while when you evaluate (intern
> "foo") you get a symbol whose name is "foo".

Ok, that's why it looked like |foo| when printed.  I thought all of Lisp 
was case-insensitive by default, but seems like that only applies to READ.

Anyway, INTERN seems cleaner than READ-FROM-STRING, so I'll go with that.

-- 
Suffering from Gates-induced brain leakage...
From: André Thieme
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <1139176540.956941.12940@g47g2000cwa.googlegroups.com>
Ulrich Hobelmann schrieb:

> Ok, that's why it looked like |foo| when printed.  I thought all of Lisp
> was case-insensitive by default, but seems like that only applies to READ.

Kent Pitman said otherwise:
http://groups.google.de/group/comp.lang.lisp/msg/7998593b3fee79b6

CL is case-sensitive and case-translating.


André
--
From: Pascal Bourguignon
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <87acd57nvr.fsf@thalassa.informatimago.com>
Ulrich Hobelmann <···········@web.de> writes:
> Ok, that's why it looked like |foo| when printed.  I thought all of
> Lisp was case-insensitive by default, but seems like that only applies
> to READ.

No that's the contrary: all of Lisp IS case-sensitive.

By default, the reader convert lowcase to upcase.  It's rather case-sensitive!

http://www.lispworks.com/documentation/HyperSpec/Body/23_aba.htm

There is no case-insensitive mode properly.

Only, since the majority of symbol names are in upcase, the :UPCASE
readtable-case setting _looks_ like it is case-insensitive. 
But it is not, never(*):


[23]> (setf (symbol-value (intern "Hello")) "World")
"World"

[24]>  (symbol-value 'hello)

*** - SYMBOL-VALUE: HELLO has no dynamic value

[26]>  (symbol-value 'Hello)

*** - SYMBOL-VALUE: HELLO has no dynamic value

[28]>  (symbol-value 'HELLO)

*** - SYMBOL-VALUE: HELLO has no dynamic value

[30]>  (symbol-value '|Hello|)
"World"

[31]> (readtable-case *readtable*)
:UPCASE


                COMMON LISP IS NOT CASE INSENSITIVE!



(*) That is, until you use implementation specific extensions that are
    not in the Common Lisp standard, but I don't speak of them.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
From: Thomas A. Russ
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <ymik6c8mil7.fsf@sevak.isi.edu>
Ulrich Hobelmann <···········@web.de> writes:

> Ok, that's why it looked like |foo| when printed.  I thought all of Lisp
> was case-insensitive by default, but seems like that only applies to
> READ.

Actually this is one of the persistent myths about Common Lisp.  It is
not CASE-INSENSITIVE.  It is always case sensitive, even the reader.  It
is just that the default action of the reader is to upcase all of the
input.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Ulrich Hobelmann
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <44mfj2F2tq4qU1@individual.net>
Ulrich Hobelmann wrote:
> Does Lisp have something like Scheme's string->symbol?

Even though it looks like bad style to me, read-from-string seems to do 
what I want.

-- 
Suffering from Gates-induced brain leakage...
From: Frank Buss
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <8wxaihusodu2$.ax92tngji2x4.dlg@40tude.net>
Ulrich Hobelmann wrote:

> Even though it looks like bad style to me, read-from-string seems to do 
> what I want.

you should be careful with all "read" functions when parsing data:

CL-USER > (read-from-string (read))
#.(format nil "~a" (+ 1 2))
3
1

but:

CL-USER > (read-from-string (read))
#.(ext:format "c:")

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Stephen Compall
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <pan.2006.02.06.04.48.16.228299@nocandysw.com>
On Sun, 05 Feb 2006 15:14:57 +0100, Ulrich Hobelmann wrote:
> Even though it looks like bad style to me, read-from-string seems to do 
> what I want.

You can also use something similar to the following:

(defun apply-readtable-case (string)
  "Convert string's case as the reader would a symbol, that is,
according to the current value of (readtable-case *readtable*)."
  (funcall (ecase (readtable-case *readtable*)
             (:upcase #'string-upcase)
             (:downcase #'string-downcase)
             (:preserve #'identity)
             ;;write your own string-invertcase and put it here
             )
           string))

and then intern the result.

-- 
Stephen Compall
Email: My username is s11.  My domain is member..org, but insert the
abbreviation for `Free Software Foundation' between the dots.
From: Ulrich Hobelmann
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <44okqoF35g4mU1@individual.net>
Stephen Compall wrote:
> On Sun, 05 Feb 2006 15:14:57 +0100, Ulrich Hobelmann wrote:
>> Even though it looks like bad style to me, read-from-string seems to do 
>> what I want.
> 
> You can also use something similar to the following:
> 
> (defun apply-readtable-case (string)
>   "Convert string's case as the reader would a symbol, that is,
> according to the current value of (readtable-case *readtable*)."
>   (funcall (ecase (readtable-case *readtable*)
>              (:upcase #'string-upcase)
>              (:downcase #'string-downcase)
>              (:preserve #'identity)
>              ;;write your own string-invertcase and put it here
>              )
>            string))
> 
> and then intern the result.

Cool, but simply string-upcasing by hand works well enough.

A cool side-effect of INTERN is that I can make all symbols part of the 
:keyword package.  Before that I got problems with EQ depending on if I 
was inside the function's package when I parsed something.  The symbol 
was only equal to the one inside the function, if the total context took 
place from inside the package.  Clearly, I'd rather import the package 
and use it from somewhere else.  Keywords help there, as they are more 
location-neutral. :)

-- 
Suffering from Gates-induced brain leakage...
From: Frank Buss
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <w0ybi1ly1a93$.17qtgk4lnn5ot.dlg@40tude.net>
Ulrich Hobelmann wrote:

> I'd like to compare some strings in a case statement for convenience, 
> but I don't know how to convert strings into symbols that are "normal".
> 
> (eq (intern "foo") 'foo) gives me NIL, so it doesn't work.  The CLHS 
> also doesn't really help me.  Make-symbol seems to be something else too.

if you don't change the readtable, characters are converted to upper case.

(defun case-test (string)
  (case (intern (string-upcase string))
    (foo "it was foo")
    (bar "was bar")
    (otherwise "not recognized")))

CL-USER > (case-test "x")
"not recognized"

CL-USER > (case-test "foo")
"it was foo"

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: ········@gmail.com
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <1139258507.060748.96520@o13g2000cwo.googlegroups.com>
Frank Buss wrote:
> (defun case-test (string)
>   (case (intern (string-upcase string))
>     (foo "it was foo")
>     (bar "was bar")
>     (otherwise "not recognized")))
>

I think find-symbol is better then intern in this case. Otherwise you
might end up creating junk symbols.

//Lennart Staflin
From: Frank Buss
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <1145eygaq53jk.7m0mr0t1acca$.dlg@40tude.net>
········@gmail.com wrote:

> I think find-symbol is better then intern in this case. Otherwise you
> might end up creating junk symbols.

thanks, good idea, because the symbol is already defined at runtime, if
used as test-keys in case.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Tim X
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <87zml54g9m.fsf@tiger.rapttech.com.au>
Ulrich Hobelmann <···········@web.de> writes:

> I'd like to compare some strings in a case statement for convenience,
> but I don't know how to convert strings into symbols that are "normal".
> 
> (eq (intern "foo") 'foo) gives me NIL, so it doesn't work.  The CLHS
> also doesn't really help me.  Make-symbol seems to be something else
> too.
> 
> Does Lisp have something like Scheme's string->symbol?
> 

I think your overlooking the fact symbols are interned as upper case
unless you use '|' i.e.

(eq (intern "foo") '|foo|) ===> T

or

(eq (intern "FOO") 'foo) ===> T

Tim

-- 
Tim Cross
The e-mail address on this message is FALSE (obviously!). My real e-mail is
to a company in Australia called rapttech and my login is tcross - if you 
really need to send mail, you should be able to work it out!
From: Thomas A. Russ
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <ymioe1kmiox.fsf@sevak.isi.edu>
Ulrich Hobelmann <···········@web.de> writes:

> I'd like to compare some strings in a case statement for convenience,
> but I don't know how to convert strings into symbols that are "normal".

But what you probably really want to do is write a nice little
STRING-CASE macro.  It shouldn't be too hard to do, just implement it in
terms of COND.

> (eq (intern "foo") 'foo) gives me NIL, so it doesn't work.  The CLHS
> also doesn't really help me.  Make-symbol seems to be something else too.

Either of these will give T

  (eq (intern "FOO") 'foo)

  (eq (intern "foo") '|foo|)

> Does Lisp have something like Scheme's string->symbol?

Well, the main choices are INTERN or MAKE-SYMBOL.


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Pascal Bourguignon
Subject: Re: String->symbol in Lisp?
Date: 
Message-ID: <87lkwo2jrx.fsf@thalassa.informatimago.com>
···@sevak.isi.edu (Thomas A. Russ) writes:

> Ulrich Hobelmann <···········@web.de> writes:
>
>> I'd like to compare some strings in a case statement for convenience,
>> but I don't know how to convert strings into symbols that are "normal".
>
> But what you probably really want to do is write a nice little
> STRING-CASE macro.  It shouldn't be too hard to do, just implement it in
> terms of COND.

It's even easier: just use google groups!

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Wanna go outside.
Oh, no! Help! I got outside!
Let me back inside!