Re: or and values

Harley Gorrell <[email protected]> Tue, 17 Oct 2006 12:07:58 -0700 (PDT)
Newsgroups gmane.lisp.allegro
Message-ID <[email protected]>
On Tue, 17 Oct 2006, Fabrizio Morbini wrote:
> 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?

    I think that was done so implementations could have a
simple "or".  You would get a better response from c.l.l.

> 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)

    Not that I know of.  One can be written though.

(defmacro or-mvl (&rest args)
   (or-mvl-1 args))

(defun or-mvl-1 (args)
   (let ((arg (car args))
         (sym (gensym)))
     `(let ((,sym (multiple-value-list ,arg)))
        (if (car ,sym)
          (values-list ,sym)
          ,(if (cdr args)
             (or-mvl-1 (cdr args))
             nil)))))

;; (macroexpand-1 '(or-mvl 1 2 3))
;; (macroexpand-1 '(or-mvl nil nil (values 1 2 3) (values 4 5 6)))

| CL-USER> (or-mvl nil nil (values 1 2 3) (values 4 5 6))
| 1
| 2
| 3

harley.