[Prev][Next][Index][Thread]
Re: zero? & 0 | 1
On Mon, 13 Mar 2000, Jason Trenouth wrote:
> On 13 Mar 2000 11:15:00 +0100, "Gabor Greif" <gabor@no.netopia.com> wrote:
>
> > I like the idiomatic expression
> >
> > bool-value? & 0 | 1
> >
> > as a shorthand for if-then-else.
> >
> > For filing a bug in an open-source Dylan implementation :-) I need
> > confirmation that the above expression evaluates to 0 for bool-value? == #t
> > and 1 for bool-value? == #f
> >
> > The reasoning is that binary operators are evaluated from left to right and
> > &, | only evaluate their rhs if necessary.
This is correct, given the additional fact that \& and \| have the same
precedence. See "operators, precedence of" and "expressions, execution
order within" in the DRM (e.g., at
<http://www.gwydiondylan.org/drm/index.html>, although the online
precendence table fails to show the lines which group operators by equal
precedence!). However, see below ...
> > Can somebody confirm this?
>
> Seems to work as you'd want in FD 2.
>
> BTW you could always wrap up the expression in a macro, e.g.
>
> ? define macro either { either(?c, ?t, ?f) } => { ?c & ?t | ?f } end macro;
This *doesn't* work in all cases; it fails precisely in the case where
"?t" evaluates to #f: then you always get the value of "?f".
Making a macro is okay but you'd want
define macro either
{ either(?c, ?t, ?f) } => { if (?c) ?t else ?f end; }
end;
OTOH, it's perhaps better to stick with the full \if expression, since
it's standard and not much longer (unless you pick a one-character name
for your macr or something!).
-- Hugh
References: