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

Re: [Scheme-reports] multiple values, and, or, when, unless

On Sun, Jan 06, 2013 at 11:34:55AM +0900, Alex Shinn wrote:
> On Sun, Jan 6, 2013 at 4:31 AM, Andy Wingo <wingo@x> wrote:
> > The specification for `and' notes that "If all the expressions evaluate
> > to true values, the value of the last expression is returned."  This is
> > not the case: as the last expression is in tail context, the _values_ of
> > the last expression are returned.  Same with `or', `when', and
> > `unless'.
> >
> Pluralized for `and'.  I'm not sure about `or' - I think it would
> always be a mistake to use MVs in an or expression.

I think there's no reason to differentiate between AND and OR in this
case.  Both return either #f or the first/last non-#f value(s).
The question is whether it makes any sense to return more than one
value in any expression.

Should (or (values 1 2) (values 3 4)) return 1 and 2?  Or is it an error?
Should (and (values 1 2) (values 3 4)) return 3 and 4?  Or is it an error?

Similarly, it's conceivable that (or #f (values 1 2)) should return
1 and 2, but in some implementations it might be regarded as an error,
just as (or #f (values 1 2) #f) could be an error.  In implementations
like Chibi which use an object to represent MV it's not an error whereas
in other implementations it might be.  In Chicken, it's not an error
either due to its behavior of discarding values beyond the first in
single-value continuations.  Guile, Gauche, Gambit and MIT Scheme allow
it too, returning all the values.  Scheme48 and Racket allow
(or #f (values 1 2)) but raise an error in the other cases.

Maybe it should be explicitly noted as producing undefined behavior.
In general, using MV in these constructs is a little weird because this
causes them to return a variable number of values: either a single #f or
all the multiple values of the first/last non-#f expression (and each
expression might return a different number of values, too!).
As you know, in Scheme this is allowed; you could use call-with-values
with a variable argument list lambda.  And I can imagine some hacks
making use of this property, but that would (and should) be explicitly


Scheme-reports mailing list