[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Binding the same variable twice in a single form



   Date: Fri, 13 Apr 90 16:47 EDT
   From: Barry Margolin <barmar@Think.COM>

       Date: Fri, 13 Apr 90 11:47:11 EDT
       From: William R. Swanson <traveler@Think.COM>

	  I can't think of any other reason why you would want to bind the same
	  variable twice in the same form, so the larger question should be moot.

       Devils' advocate: How about the following (obviously kludgy) example?

       (defun get-a-number ()
	 (let* ((number (read))
		(number (if (numberp number) number 0)))
	    (print number)))

       It may not be nice programming style, but it works...

   Your example uses sequential binding, whose semantics in this case are
   obvious (although I'm not sure the ANSI draft specifically mentions
   this).  I was talking about parallel binding.

Hmph! Okay, how about:

(defun get-info-function ()
   ...
   (values unimportant may-be-used will-be-used))

(defun process-all-info (function)
   (multiple-value-bind (a b c)
        (funcall function)
   <code that uses a b & c>
   ))
(defun process-important-info (function)
   (multiple-value-bind (value value other-value)
   	(funcall function)
   <code that uses both value and other-value>
   ))

(process-important-info #'get-info-function)

Of course, this example is starting to get a little strained, assuming
as it does an attempt to create a generic function-calling interface.
What's bothersome is that, on a Lispm at least, this code doesn't do
the right thing. For example,

(multiple-value-bind (a a b) (values 2 3 4) (print a) (print b))

prints out 2 and 4, not 3 and 4 as one might expect.

--- WRS