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...
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/
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...
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é
--
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
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
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...
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
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.
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...
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
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
········@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
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!
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
···@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!