home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / lisp / 3312 < prev    next >
Encoding:
Text File  |  1993-01-21  |  1.8 KB  |  64 lines

  1. Newsgroups: comp.lang.lisp
  2. Path: sparky!uunet!stanford.edu!lucid.com!karoshi!fy
  3. From: fy@lucid.com (Frank Yellin)
  4. Subject: Re: (SETF VALUES)
  5. In-Reply-To: jeff@aiai.ed.ac.uk's message of 19 Jan 93 20:20:19 GMT
  6. Message-ID: <FY.93Jan21100318@hardwick.lucid.com>
  7. Sender: usenet@lucid.com
  8. Organization: Lucid, Inc., Menlo Park, CA
  9. References: <FY.93Jan6155542@hardwick.lucid.com> <SJAMESON.93Jan8084615@fergie.atl.ge.com>
  10.     <1imgf4INNmf@early-bird.think.com> <8204@skye.ed.ac.uk>
  11. Date: 21 Jan 93 10:03:18
  12. Lines: 50
  13.  
  14.  
  15. In article <8204@skye.ed.ac.uk> jeff@aiai.ed.ac.uk (Jeff Dalton) writes:
  16. >
  17. >> The normal multiple-value assignment operators only allow you to assign to
  18. >> variables, but you might want to assign to a set of generalized references.
  19. >> Here's a simple, contrived example:
  20. >>
  21. >> (defstruct (q-and-r (:conc-name qr-))
  22. >>  quotient
  23. >>  remainder)
  24. >>
  25. >> (defvar *qr* (make-q-and-r))
  26. >> ...
  27. >> (setf (values (qr-quotient *qr*) (qr-remainder *qr*))
  28. >>      (floor numerator denominator))
  29. >>  So a multiple-value-setf would have worked as well?
  30.  
  31.  
  32. Yes, it is true that
  33.     (setf (values <x> <y> <z>) <something>)
  34. Would be equivalent to 
  35.     (multiple-value-setf (<x> <y> <z>) <something>)
  36.  
  37. The advantage of having a setf method for values is that it allows you to
  38. use #'values in macros.
  39.  
  40. Let me give a slightly contrived example.  Imagine you have some sort of
  41. data type PAIR for holding two values.
  42.  
  43. You might want to do operations like:
  44.  
  45.    > (setq x (make-pair 3 5))
  46.    #<Pair>
  47.    > (pair-value x)
  48.    3
  49.    5
  50.    (setf (pair-value x) (truncate 10 3))
  51.    3
  52.    1
  53.    
  54. A trivial implementation would be:
  55.  
  56.    (defun make-pair (x y) (cons x y))
  57.  
  58.    (defmacro pair-value (x) `(values (car ,x) (cdr ,x)))
  59.  
  60. [Yes, this is a contrived example.  Yes, I realise that x is evaluated
  61. twice in the definition of pair-value.]
  62.  
  63. -- Frank Yellin
  64.