home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!decwrl!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.lisp
- Subject: Re: Want to prepend colon to symbol
- Message-ID: <14mmooINNm20@early-bird.think.com>
- Date: 23 Jul 92 16:29:12 GMT
- References: <BruGyy.HDF@news.cso.uiuc.edu> <BruJ08.I0s@news.cso.uiuc.edu>
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 48
- NNTP-Posting-Host: telecaster.think.com
-
- 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 <BruJ08.I0s@news.cso.uiuc.edu> jmrg9881@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.
-
- barmar@think.com {uunet,harvard}!think!barmar
-