Thread overview
a<b<c?
Feb 12, 2005
xs0
Feb 12, 2005
zwang
Feb 13, 2005
xs0
February 12, 2005
Hi!

I always wondered why no language includes such syntax (of those I know, at least), but wouldn't it be possible to have this?

if (a < b <= c < d) ...

It looks much more obvious (and prettier and shorter) than

if ((a < b) && (b <= c) && (c < d)) ...

And this is also a common mathematical notation, afaik..

It is perfectly OK, if it is evaluated just the same as the long form (with short-circuiting and everything).

I'm not sure whether to allow this:

if (a < b > c)

But I guess there is no harm, again it gets translated to (a<b) && (b>c)..



xs0
February 12, 2005
xs0 wrote:
> Hi!
> 
> I always wondered why no language includes such syntax (of those I know, at least), but wouldn't it be possible to have this?
> 
> if (a < b <= c < d) ...

This is a perfectly legal expression in D and many other languages,
though semantically equivalent to (((a<b)<=c)<d) rather than
((a<b)&&(b<=c)&&(c<d)).


> 
> It looks much more obvious (and prettier and shorter) than
> 
> if ((a < b) && (b <= c) && (c < d)) ...
> 
> And this is also a common mathematical notation, afaik..
> 
> It is perfectly OK, if it is evaluated just the same as the long form (with short-circuiting and everything).
> 
> I'm not sure whether to allow this:
> 
> if (a < b > c)
> 
> But I guess there is no harm, again it gets translated to (a<b) && (b>c)..
> 
> 
> 
> xs0
February 12, 2005
Recently, in college, I have done an intermediate compiler, that compiled a
language similar
to Matlab to triple address code (3ac).

When I was doing the grammar that question came up too!

As you said, mathematicians use the notation: a < b < c, which means that b belongs to the open interval ]a , c [ and is equivalent to (a < b) && (b < c).

However, imho, I think there is some ambiguity, because programming
languages implement boolean expressions and it can have another meaning:
     (a < b) < c.
Which evaluates to: ((a < b) ? 1 : 0) < c

Miguel Ferreira Simoes


February 13, 2005
>> if (a < b <= c < d) ...
> 
> 
> This is a perfectly legal expression in D and many other languages,
> though semantically equivalent to (((a<b)<=c)<d) rather than
> ((a<b)&&(b<=c)&&(c<d)).

Well, I know that, but wouldn't it be much more natural if it was the second form? I can't think of a case where I'd want it compiled as (((a<b)<=c)<d), while the other case happens very frequently.. I think if this is something the parser can easily handle, it'd be a nice feature to have..


xs0