home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / lisp / 2088 < prev    next >
Encoding:
Text File  |  1992-07-23  |  2.3 KB  |  60 lines

  1. Path: sparky!uunet!olivea!decwrl!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!think.com!barmar
  2. From: barmar@think.com (Barry Margolin)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: Want to prepend colon to symbol
  5. Message-ID: <14mmooINNm20@early-bird.think.com>
  6. Date: 23 Jul 92 16:29:12 GMT
  7. References: <BruGyy.HDF@news.cso.uiuc.edu> <BruJ08.I0s@news.cso.uiuc.edu>
  8. Organization: Thinking Machines Corporation, Cambridge MA, USA
  9. Lines: 48
  10. NNTP-Posting-Host: telecaster.think.com
  11.  
  12. I sent the following in direct mail to Jon (since he said he doesn't read
  13. the newsgroup), but I thought it also deserved to be published here.
  14.  
  15. In article <BruJ08.I0s@news.cso.uiuc.edu> jmrg9881@uxa.cso.uiuc.edu (Jon Reid) writes:
  16. >Never mind, hold all the e-mail responses.  READ-FROM-STRING is my friend.
  17. I suggest you use my version, as READ-FROM-STRING can screw up in unusual
  18. cases.  Consider the following three definitions of PREPEND-COLON
  19.  
  20. (defun prepend-colon-1 (symbol)
  21.   (read-from-string (format nil ":~A" symbol)))
  22.  
  23. (defun prepend-colon-2 (symbol)
  24.   (read-from-string (format nil ":~S" symbol)))
  25.  
  26. (defun prepend-colon-3 (symbol)
  27.   (intern (symbol-name symbol) "KEYWORD"))
  28.  
  29. (defmacro with-error-object (&body body)
  30.   `(handler-case (progn ,@body)
  31.      (error (e) (format nil "~A" e))))
  32.  
  33. (defun test-pc (&rest symbols)
  34.   (dolist (symbol symbols)
  35.     (format t "~&~S~15T ~S~25T ~S~40T ~S~%" symbol
  36.       (with-error-object (prepend-colon-1 symbol))
  37.       (with-error-object (prepend-colon-2 symbol))
  38.       (with-error-object (prepend-colon-3 symbol))))
  39.   (values))
  40.  
  41. (make-package 'barmar)
  42.  
  43. (test-pc '|foo| '|FOO BAR| 'barmar::foo 'barmar::|foo|)
  44.  
  45. |foo|           :FOO      :|foo|         :|foo|
  46. |FOO BAR|       :FOO      :|FOO BAR|     :|FOO BAR|
  47. BARMAR::FOO     :FOO      #<READER-ERROR {71009DD}>  :FOO
  48. BARMAR::|foo|   :FOO      #<READER-ERROR {710241D}>  :|foo|
  49.  
  50. Notice that PREPEND-COLON-1 loses the case of the symbol and can't handle
  51. symbols whose name contain delimiters.  PREPEND-COLON-2 gets those right,
  52. but it loses if the symbol isn't accessible in the current package because
  53. it tries to parse something like ":BARMAR::FOO", which is invalid symbol
  54. syntax.  PREPEND-COLON-3 always gets these things right.
  55. -- 
  56. Barry Margolin
  57. System Manager, Thinking Machines Corp.
  58.  
  59. barmar@think.com          {uunet,harvard}!think!barmar
  60.