Page 1 of 1

Use assoc with argument of type string

Posted: Sun Jun 17, 2012 5:00 am
by zcohen
Hi everybody,
I have this association-list:

Code: Select all

(defvar base-list (list (cons 'a 0) (cons 2 'c)))
I have to call assoc when my argument is of type string.

So for the pair

Code: Select all

 (A . 0)
I have to convert "a" to a symbol, and for the pair

Code: Select all

(2 . C)
I have to convert "2" to a symbol. How can I do that?

This should work like this:

Code: Select all

CL-USER 28 : 1 > (assoc (convert-string-to-symbol "a") base-list)
(A . 0)
CL-USER 28 : 1 > (assoc (convert-number-to-symbol "2") base-list)
(2 . C)
I tried using intern but got NIL:

Code: Select all

CL-USER 29 : 1 > (assoc (intern "a") base-list)
NIL
Thanks a lot

Re: Use assoc with argument of type string

Posted: Sun Jun 17, 2012 6:52 am
by zcohen
ou were close with intern; you just had the case wrong. Try this:

> (assoc (intern "A") base-list)
(A . 0)
Note that here the name-as-string is capitalized.

Alternately, you could use find-symbol to look for an existing symbol by name:

> (assoc (find-symbol "A") base-list)
(A . 0)
The key here is that when you wrote your original defvar form, the reader read the string "a" and—by virtue of the current readtable case—converted the symbol name to be uppercase. Symbols with names of different case are not equal. It just so happens that at read time the reader is projecting what you wrote (lowercase) to something else (uppercase).

You can inspect the current case conversion policy for the current reader using the readtable-case function:

> (readtable-case *readtable*)
:UPCASE
To learn more about how the readtable case and the reader interact, see the discussion in section 23.1.2 of the Hyperspec.

Re: Use assoc with argument of type string

Posted: Sun Jun 17, 2012 6:58 am
by Goheeca
Because CL is internally case-sensitive and ordinary symbols (without vertical bars) are in upper case. Thus this will work:

Code: Select all

(assoc (intern "A") base-list)
Furthermore an ordinary symbol can't have digit chars at beginning.
The cons (2 . C) can be got by:

Code: Select all

(assoc 2 base-list)
// Result of

Code: Select all

(intern "a")
is a symbol |a| and simirlarly

Code: Select all

(intern "2")
is a symbol |2|.