Use assoc with argument of type string

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.

Use assoc with argument of type string

Postby zcohen » Sun Jun 17, 2012 5:00 am

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
zcohen
 
Posts: 6
Joined: Wed Jun 06, 2012 12:41 am

Re: Use assoc with argument of type string

Postby zcohen » Sun Jun 17, 2012 6:52 am

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.
zcohen
 
Posts: 6
Joined: Wed Jun 06, 2012 12:41 am

Re: Use assoc with argument of type string

Postby Goheeca » Sun Jun 17, 2012 6:58 am

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|.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 213
Joined: Thu May 10, 2012 12:54 pm


Return to Homework

Who is online

Users browsing this forum: No registered users and 2 guests

cron