home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.lisp
- Path: sparky!uunet!stanford.edu!lucid.com!karoshi!fy
- From: fy@lucid.com (Frank Yellin)
- Subject: Re: (SETF VALUES)
- In-Reply-To: jeff@aiai.ed.ac.uk's message of 19 Jan 93 20:20:19 GMT
- Message-ID: <FY.93Jan21100318@hardwick.lucid.com>
- Sender: usenet@lucid.com
- Organization: Lucid, Inc., Menlo Park, CA
- References: <FY.93Jan6155542@hardwick.lucid.com> <SJAMESON.93Jan8084615@fergie.atl.ge.com>
- <1imgf4INNmf@early-bird.think.com> <8204@skye.ed.ac.uk>
- Date: 21 Jan 93 10:03:18
- Lines: 50
-
-
- In article <8204@skye.ed.ac.uk> jeff@aiai.ed.ac.uk (Jeff Dalton) writes:
- >
- >> The normal multiple-value assignment operators only allow you to assign to
- >> variables, but you might want to assign to a set of generalized references.
- >> Here's a simple, contrived example:
- >>
- >> (defstruct (q-and-r (:conc-name qr-))
- >> quotient
- >> remainder)
- >>
- >> (defvar *qr* (make-q-and-r))
- >> ...
- >> (setf (values (qr-quotient *qr*) (qr-remainder *qr*))
- >> (floor numerator denominator))
- >> So a multiple-value-setf would have worked as well?
-
-
- Yes, it is true that
- (setf (values <x> <y> <z>) <something>)
- Would be equivalent to
- (multiple-value-setf (<x> <y> <z>) <something>)
-
- The advantage of having a setf method for values is that it allows you to
- use #'values in macros.
-
- Let me give a slightly contrived example. Imagine you have some sort of
- data type PAIR for holding two values.
-
- You might want to do operations like:
-
- > (setq x (make-pair 3 5))
- #<Pair>
- > (pair-value x)
- 3
- 5
- (setf (pair-value x) (truncate 10 3))
- 3
- 1
-
- A trivial implementation would be:
-
- (defun make-pair (x y) (cons x y))
-
- (defmacro pair-value (x) `(values (car ,x) (cdr ,x)))
-
- [Yes, this is a contrived example. Yes, I realise that x is evaluated
- twice in the definition of pair-value.]
-
- -- Frank Yellin
-