home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / lisp / 2414 < prev    next >
Encoding:
Text File  |  1992-09-11  |  2.8 KB  |  66 lines

  1. Newsgroups: comp.lang.lisp
  2. Path: sparky!uunet!usc!elroy.jpl.nasa.gov!ufo!Aig.Jpl.Nasa.Gov!charest
  3. From: charest@Aig.Jpl.Nasa.Gov (Len Charest)
  4. Subject: Re: Function to print in #S format
  5. Message-ID: <1992Sep12.002011.20807@jpl-devvax.jpl.nasa.gov>
  6. Followup-To: comp.lang.lisp
  7. Sender: usenet@jpl-devvax.jpl.nasa.gov (For NNTP so rrn will be able to post)
  8. Nntp-Posting-Host: ai-cyclops
  9. Reply-To: charest@aig.jpl.nasa.gov
  10. Organization: NASA/Jet Propulsion Laboratory
  11. References: <SJAMESON.92Sep8161926@fergie.dnet.ge.com> <LIBERTE.92Sep9132646@birch.cs.uiuc.edu>
  12. Date: Sat, 12 Sep 1992 00:20:11 GMT
  13. Lines: 50
  14.  
  15. In article <LIBERTE.92Sep9132646@birch.cs.uiuc.edu>, liberte@cs.uiuc.edu (Daniel LaLiberte) writes:
  16. |> In article <1992Sep9.011022.14516@jpl-devvax.jpl.nasa.gov> charest@Aig.Jpl.Nasa.Gov (Len Charest) writes:
  17. |> 
  18. |>    3. (mutually exclusive) Use CLOS. Ha Ha only serious.
  19. |> 
  20. |> 
  21. |> I took this suggestion seriously.  It wasnt obvious (to me) at first
  22. |> how one is supposed to print an object readably.  I wrote the
  23. |> following to help.  Please comment.
  24.     [ sample code deleted]
  25.  
  26. MAKE-LOAD-FORM (CLtL2, p659) could be exploited to provide readable printed representations for structures and CLOS instances. I believe you would still need an :around method on PRINT-OBJECT to test *PRINT-READABLY* and explicitly invoke MAKE-LOAD-FORM. The real benefit of MAKE-LOAD-FORM is that it allows the programmer to decide the meaning of 'similar as a constant' for structures and CLOS instances. (Recall that objects printed while *PRINT-READABLY* is true must be similar as a constant.) CLtL2 gives 
  27.  
  28.  
  29. several examples. In the first, the notion of similar as a constant is satisfied by instantiating a new instance with the same slot values as the original instance. This is the same approach used in Dan LaLiberte's code. 
  30.  
  31. As a parting shot, I'd like to point out that readable printed representations for structures (or any other object for that matter) don't necessarily have to use #S syntax. You could define a new # dispatch macro reader that simply read back in as a form to 'find' the printed object in some global cache. For example:
  32.  
  33. (defvar *bangs* nil)
  34.  
  35. (defstruct (bang
  36.          (:print-function print-bang)
  37.          (:constructor make-bang-1 (name)))
  38.   name)
  39.  
  40. (defun make-bang (name)
  41.   (let ((b (make-bang-1 name)))
  42.     (push b *bangs*)
  43.     b))
  44.  
  45. (defun print-bang (b s d)
  46.   (declare (ignore d))
  47.   (format s "#!~s"
  48.       (bang-name b)))
  49.  
  50. (defun bang-reader (s sub arg)
  51.   (declare (ignore sub arg))
  52.   `(find ',(read s t nil t) *bangs*))
  53.  
  54. (set-dispatch-macro-character #\# #\! #'bang-reader)
  55.  
  56. > (make-bang 'pow)
  57. #!POW
  58. > #!wow
  59. NIL
  60. > #!POW
  61. #!POW
  62. ..................................................
  63.                                   Len Charest, Jr.
  64.                  JPL Artificial Intelligence Group
  65.                           charest@aig.jpl.nasa.gov
  66.