Re: or and values
Andrew Philpot <[email protected]> Tue, 17 Oct 2006 11:30:20 -0700
| Newsgroups | gmane.lisp.allegro |
|---|---|
| Message-ID | <[email protected]> |
Hi, i found rather unintuitive the behaviour of OR with values. Why is
it the case that only if the values form is the last then all the
values are returned?
For example, given the code:
(or (values 2 3) nil) i would like to see as returned values 2 and 3,
but i get only 2 as specified in the HyperSpecs.
Is there a way to get all the values of the first form returning a non
nil primary value? (without using conditionals and local variables)
Thanks, Fabrizio.
Yeah, I think it's required by the standard.
Here's the macroexpansion of OR in terms of COND:
CL-USER(11): (macroexpand-1 '(or (a) (b)))
(COND ((A))
(T (B)))
T
I once found this same issue unintuitive, and wrote the following:
(defmacro mvor (&rest exprs)
(let ((v (gensym "V")))
(if (null exprs)
nil
`(let ((,v (multiple-value-list ,(car exprs))))
(if (car ,v)
(values-list ,v)
(mvor ,@(cdr exprs)))))))
which considers (values nil t) to be false (??) and which which yields
USER(15): (macroexpand '(mvor (a) (b)))
(LET ((#:V44653 (MULTIPLE-VALUE-LIST (A))))
(IF (CAR #:V44653)
(VALUES-LIST #:V44653)
(MVOR (B))))
T
Don't know whether its use of conditionals and variables is acceptable
for you. Of course, the built-on OR might essentially do the same
under the hood, as we saw, since special forms can use the
macroexpansion if they like.
As I scan my code base, I used it approximately once.
Andrew