Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!gatech!mcnc!decvax!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.misc Subject: Re: What I'd really like to see in an if-statement... Summary: How to implement "a < b < c" Message-ID: <14278@haddock.ima.isc.com> Date: 13 Aug 89 18:02:20 GMT References: <8577@batcomputer.tn.cornell.edu> <14251@haddock.ima.isc.com> <516@brazos.Rice.edu> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 41 In article <516@brazos.Rice.edu> preston@titan.rice.edu (Preston Briggs) writes: >In article <14251@haddock.ima.isc.com> karl@haddock (Karl Heuer) writes: >>which parses "a < x < b" as a ternary operator, > >But you wouldn't want a single production to get ayou really want to be able to handle a < b < c < ... < n True; I was oversimplifying. I'll cover the general case below. >On the other hand, versions of LISP allow things like > (< a b c d) That doesn't completely solve the problem; I want to be able to write 0 <= i < N without having to throw in +1/-1 on half of the expressions. (I consider a half-open interval to be the more natural endpoint-representation of a range.) Okay, then, here's one way to parse such constructs. A comparison operator like "a < b" returns an object of an internal type "cascadable boolean", whose value is either {FALSE} or {TRUE, b}. If a cascadable boolean is used in any context other than the left operand of a compare, it immediately decays into a normal boolean. If it is the left operand of a compare, then "{FALSE} < c" returns {FALSE} (but should probably still evaluate c for side effects), while "{TRUE, b} < c" returns the same object as "b < c" (another cascadable boolean). This would apply to "==" and "!=" as well as "<" "<=" ">" ">=", and I would make all six operators have the same precedence. (The only reason for having separate precedences for equality and relational operators is to allow a < b == c < d /* do the two relations have the same truth-value? */ , which would still be legal if properly parenthesized -- enclosing a cascadable boolean in parenthesis would also force the decay into a normal boolean.) (The above is an example of how to implement it, and perhaps how it would have to be defined in the official language specs. The language reference manual can simply say that "expr relop expr relop ... relop expr" works the way a mathematician might expect, and the whole construct returns a single boolean.) Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint