[comp.lang.lisp.x] Can I do multiple value returns in X-Lisp?

tony@oha.UUCP (Tony Olekshy) (02/01/91)

I am trying to write a macro that sets the value of each symbol in a list
of symbols to the corresponding value from a list of values (presumably
returned by a function.  But when I try the following, the global foo and
bar are set, not those local to the let, which is what I want.  Reading
leads me to believe that this a property of set, so I have tried screwing
around with setq to no avail.  Does anyone have a lantern?

	(defmacro setset (names vals)
	    `(mapcar (lambda (n v) (set n v)) ,names ,vals))

	(defun baz () (list 234 436))

	(let (foo bar)
	    (setset '(foo bar) (baz))
	    (print (list foo bar))
	    )

	(print (list foo bar))

--
Yours etc., Tony Olekshy.       Internet: tony%oha@CS.UAlberta.CA
				  BITNET: tony%oha.uucp@UALTAMTS.BITNET
				    uucp: alberta!oha!tony
Where the spirit does not work with the hand there is no art. --Da Vinci

riesbeck@ils.nwu.edu (Chris Riesbeck) (02/02/91)

In article <470@oha.UUCP>, tony@oha.UUCP (Tony Olekshy) writes:
> I am trying to write a macro that sets the value of each symbol in a list
> of symbols to the corresponding value from a list of values (presumably
> returned by a function.  But when I try the following, the global foo and
> bar are set, not those local to the let, which is what I want.  Reading
> leads me to believe that this a property of set, so I have tried screwing
> around with setq to no avail.  Does anyone have a lantern?
> 
> 	(defmacro setset (names vals)
> 	    `(mapcar (lambda (n v) (set n v)) ,names ,vals))
> 
> 	(defun baz () (list 234 436))
> 
> 	(let (foo bar)
> 	    (setset '(foo bar) (baz))
> 	    (print (list foo bar))
> 	    )

SET, like EVAL, only works on globals. There's a better way to do this,
by defining something that looks like Common Lisp's multiple value forms.

(defun values (&rest args) args)   ;;for clarity

(defmacro multiple-value-bind (vars exp &rest body)
 `(apply #'(lambda ,vars ,@body) ,exp))

(defun baz () (values 234 436))

(multiple-value-bind (foo bar) (baz)
  (print (list foo bar)))

=> (234 436)

The nice thing about this is that if you move to Common Lisp, you're
code could potentially be more efficient, no longer constructing and
deconstructing lists. 

Chris Riesbeck
--------------