Can anyone think of a way to prepend a colon to a symbol, i.e., convert foo
to :foo ? I want to use this in a macro.
(format nil ":~a" 'foo) yields ":foo", a string. If I could convert this
string back to a symbol, I'd be a happy camper.
Please respond by e-mail as I do not subscribe to this group.
-- Jon Reid (······@uiuc.edu)
Never mind, hold all the e-mail responses. READ-FROM-STRING is my friend.
Thanks to Mike Perkowitz for a very quick response. And now, I can proceed
with my programming...
-- Jon (······@uiuc.edu)
I feel kind of silly doing all the follow-ups to my own question, but
that's what I get for not subscribing to this group. It also keeps
redundant e-mail down, and gives me more refined answers.
There is a more elegant solution to my problem than using read-from-string.
It is: (intern foo 'keyword).
Thanks to everyone for your help!
-- Jon (······@uiuc.edu)
From: David F. Skoll
Subject: Re: Want to prepend colon to symbol
Date:
Message-ID: <dfs.711906870@ro>
In <··········@news.cso.uiuc.edu> ········@uxa.cso.uiuc.edu (Jon Reid) writes:
>There is a more elegant solution to my problem than using read-from-string.
>It is: (intern foo 'keyword).
There's confusion here between the print name of a symbol, and the
package in which the symbol is found.
The symbol :FOO has the print name "FOO" and is found in the keyword package.
The symbol |:FOO| has the print name ":FOO" and we can't tell what
package it's in without further information. (Probably the
COMMON-LISP-USER package)
The symbol :|:FOO| has the print name ":FOO" and is found in the
keyword package. :-)
--
David F. Skoll
I sent the following in direct mail to Jon (since he said he doesn't read
the newsgroup), but I thought it also deserved to be published here.
In article <··········@news.cso.uiuc.edu> ········@uxa.cso.uiuc.edu (Jon Reid) writes:
>Never mind, hold all the e-mail responses. READ-FROM-STRING is my friend.
I suggest you use my version, as READ-FROM-STRING can screw up in unusual
cases. Consider the following three definitions of PREPEND-COLON
(defun prepend-colon-1 (symbol)
(read-from-string (format nil ":~A" symbol)))
(defun prepend-colon-2 (symbol)
(read-from-string (format nil ":~S" symbol)))
(defun prepend-colon-3 (symbol)
(intern (symbol-name symbol) "KEYWORD"))
(defmacro with-error-object (&body body)
`(handler-case (progn ,@body)
(error (e) (format nil "~A" e))))
(defun test-pc (&rest symbols)
(dolist (symbol symbols)
(format t "~&~S~15T ~S~25T ~S~40T ~S~%" symbol
(with-error-object (prepend-colon-1 symbol))
(with-error-object (prepend-colon-2 symbol))
(with-error-object (prepend-colon-3 symbol))))
(values))
(make-package 'barmar)
(test-pc '|foo| '|FOO BAR| 'barmar::foo 'barmar::|foo|)
|foo| :FOO :|foo| :|foo|
|FOO BAR| :FOO :|FOO BAR| :|FOO BAR|
BARMAR::FOO :FOO #<READER-ERROR {71009DD}> :FOO
BARMAR::|foo| :FOO #<READER-ERROR {710241D}> :|foo|
Notice that PREPEND-COLON-1 loses the case of the symbol and can't handle
symbols whose name contain delimiters. PREPEND-COLON-2 gets those right,
but it loses if the symbol isn't accessible in the current package because
it tries to parse something like ":BARMAR::FOO", which is invalid symbol
syntax. PREPEND-COLON-3 always gets these things right.
--
Barry Margolin
System Manager, Thinking Machines Corp.
······@think.com {uunet,harvard}!think!barmar