vaughan@puma.cad.mcc.com (Paul Vaughan) (04/22/89)
This is a specification for a multiple-value handling OR construct. It behaves like the traditional OR, but returns all the values of the first form which returns a non-nil first value. (The usual OR does this only if the first such form is the last form, otherwise, it returns only the first value.) (defmacro mv-or (&rest forms) (cond ((null forms) nil) ((null (cdr forms)) (first forms)) (t (let ((values-symbol (gensym "VALUES"))) `(let ((,values-symbol (multiple-value-list ,(first forms)))) (if (car ,values-symbol) (values-list ,values-symbol) (mv-or . ,(cdr forms)))))))) I might use it in a function that did something like (defun lookup (key primary-table secondary-table) (declare (values value found found-key)) (mv-or (gethash key primary-table) (gethash key secondary-table))) Somebody please tell me why such a thing is stupid, unnecessary, or should never arise. Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639 Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan
barmar@think.COM (Barry Margolin) (04/24/89)
In article <886@cadillac.CAD.MCC.COM> vaughan@puma.cad.mcc.com (Paul Vaughan) writes: >This is a specification for a multiple-value handling OR construct. ... >Somebody please tell me why such a thing is stupid, unnecessary, or >should never arise. As you've shown, there's a perfectly good use for such a construct. Perhaps your real question is why the built-in OR construct doesn't behave this way. For the same reason that there's a PROG1 and a MULTIPLE-VALUE-PROG1: allocating space for a single temporary is easier than doing it for arbitrary-length temporaries. Barry Margolin Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar
hoey@ai.etl.army.mil (Dan Hoey) (04/26/89)
In article <886@cadillac.CAD.MCC.COM> vaughan@puma.cad.mcc.com (Paul Vaughan) writes of a multiple-value OR, specified to return all the values of the first form that returns a non-NIL first value. He motivates it with >(defun lookup (key primary-table secondary-table) > (declare (values value found found-key)) > (mv-or (gethash key primary-table) (gethash key secondary-table))) But in this case we probably want a MV-OR that returns the values of the first form to return a non-NIL *second* value. Furthermore, using a generalized MV-OR requires MULTIPLE-VALUE-LIST, which conses; for combining results of GETHASH we prefer MULTIPLE-VALUE-BIND or MULTIPLE-VALUE-SETQ, which generate no garbage but must be written with knowledge of the number of values to handle. Dan