On Tue, Aug 29, 2006 at 08:55:09AM +0900, Michael W. Ryder wrote:
P.
Smalltalk evaluates and and or operators the same way. Most evaluation
is done from left to right with only a few different priorities.
Yes, and this is consistant behaviour for Smalltalk, as Smalltalk uses
left to right evaluation even for +, * arithmetic.
I wouldn’t say it is wrong the way ruby does it, but not exactly
intuitive either. Choosing to follow english language rather than
widespread convention and similiar constructs in ruby itself (&&,
||, +, *, …) may seem a bit … anti POLS.
I feel this is a true case of POLS violation, not one of those “I
don’t know shit, and I was surprised” examples. I do know about
operator precedence in ruby in general, and was surprised by this
special case. Never ran into it myself though, probably because of
luck or spurious parentheses.
It’s not a major deal. It certainly can’t be changed in 1.8 without
breaking code. Who feels this is worth changing in 1.9?
On Tue, Aug 29, 2006 at 08:55:09AM +0900, Michael W. Ryder wrote:
Smalltalk evaluates and and or operators the same way. Most evaluation
is done from left to right with only a few different priorities.
Yes, and this is consistant behaviour for Smalltalk, as Smalltalk uses
left to right evaluation even for +, * arithmetic.
Smalltalk only prioritizes between
unary selectors which bind the tightest
binary selectors which are next (so a negated + b is (a negated) +
b
keyword selectors which are last
object jump: x + y * z towards: fred location
is interpreted as
object jump: ((x + y) * z) towards: (fred location)
I wouldn’t say it is wrong the way ruby does it, but not exactly
intuitive either. Choosing to follow english language rather than
widespread convention and similiar constructs in ruby itself (&&,
||, +, *, …) may seem a bit … anti POLS.
A perl programmer would be very surprised if AND, OR, && and || worked
any other way. Probably the most common perl idiom here is:
"some long complicated expression" or die
I feel this is a true case of POLS violation, not one of those “I
don’t know shit, and I was surprised” examples. I do know about
operator precedence in ruby in general, and was surprised by this
special case. Never ran into it myself though, probably because of
luck or spurious parentheses.
It’s often pointed out that it’s impossible to minimize EVERYONE’s
surprise.
It’s not a major deal. It certainly can’t be changed in 1.8 without
breaking code. Who feels this is worth changing in 1.9?
On Tue, Aug 29, 2006 at 08:55:09AM +0900, Michael W. Ryder wrote:
I wouldn’t say it is wrong the way ruby does it, but not exactly
intuitive either. Choosing to follow english language rather than
widespread convention and similiar constructs in ruby itself (&&,
||, +, *, …) may seem a bit … anti POLS.
A perl programmer would be very surprised if AND, OR, && and || worked
any other way. Probably the most common perl idiom here is:
"some long complicated expression" or die
That’s not a complete example, as no precedence rules come into play
here. and and or are pretty low-precedence operators in perl and ruby
alike, which makes constructs like the above possible without
parenthesises. But what I am driving at is this:
In the first case, && binds stronger than ||, but is short circuited
away. In the second case “and” does not bind stronger than “or”, which
suprised me, given prior knowledge. Is it really only me?
Why the and and or operators have the same priority? Unlike && and ||
and also unlike the most of, or maybe all, other languages and conventions?
Good question.
irb(main):001:0> true and false or true and false
=> false
tells me that and binds tighter than or, unless something unobvious is
going on, which might well be…
Just. Use. Parentheses. Even in programming languages where the rules
are clear. Explicit >> Implicit.
More parentheses, less readability. E.g. the natural language is quite
well understandable without the parentheses — and (((I’m) sure),
(((it’s) (better understandable)) (than (it (((would be) understandable)
(with them)))))). May be its operator precedence is better designed than
the ruby’s one? (Although it can be hardly believable that something
could be even better than ruby.
(Yes, long time spent on that now, who would expect such a
or now that that’s been brought up.
Is that actually a mathematical rule, though? I don’t remember as
such from any math or logic that I know, and poking around through my
old textbooks made no mention of binding or precedence of logical
operations; the closest thing being a statement that parentheses are
required to avoid ambiguity when mixing and and or in one statement,
at least slightly implying that there is no ‘order of operations’.
If anyone has a reference either way on this, I’d be curious to see it.
are. We should probably find out what it is before suggesting it be
changed.
The general idea is that you can use and and or in similiar places as if
and
unless modifiers (ala “or die” in perl, at least as far as I understand
it)
e.g. x = string.index(“h”) or raise “No h in string!”
Also with left to right equal precendence you can use them as a ternay
operator:
More parentheses, less readability. E.g. the natural language is quite
well understandable without the parentheses — and (((I’m) sure),
(((it’s) (better understandable)) (than (it (((would be) understandable)
(with them)))))). May be its operator precedence is better designed than
the ruby’s one? (Although it can be hardly believable that something
could be even better than ruby.
And for Christmas, I want a pony or a doggy and rollerskates and a bike
or a computer and an action figure and a toy car or a goldfish or new
shoes and a TV.
So…
Perfectly understandable? Only if people naturally follow boolean
algebra rules when talking. (Riiight.) Of course, the above sentence is
particularly convoluted (making it no more a contrived example than
yours I’d say though), that’s why it’s pretty much impossible to
determine what it’s supposed to say. Which is my point: if you want
programming language code as readable as natural language, just using
words instead of symbols doesn’t cut it - you should phrase your
expressions clearly nonetheless. Like, oh, without long tangles of ands
and ors.
For what it’s worth though, since most programmers DO expect
mathemathical rules to hold in programming languages, for the sake of
consistency, I agree that the and should bind more tightly than or now
that that’s been brought up. I just don’t really consider it critical
since I believe you should never mix those in a single expression at all
boolean expressions are for me much easier to visually parse as “one
must be true” or “all must be true” when you don’t have nested anonymous
complex terms involved. Those are a good candidate for extraction into a
local variable or a separate predicate method.
conventions?
mathemathical rules to hold in programming languages, for the sake of
If anyone has a reference either way on this, I’d be curious to see it.
matthew smillie.
My rather dim recollection is that computer jocks forced “and” to bind
tighter than “or” and “not” to bind tighter than “and” just so it was
defined. Only in programming (and logic design) do you usually find
Boolean expressions complicated enough for operator precedence to
matter.
Meanwhile, the logicians pretty much abandoned parentheses for Polish
(prefix) notation. And of course, the computer jocks, not to be outdone,
invented reverse Polish notation.
My rather dim recollection is that computer jocks forced “and” to bind
tighter than “or” and “not” to bind tighter than “and” just so it was
defined. Only in programming (and logic design) do you usually find
Boolean expressions complicated enough for operator precedence to matter.
Hmm. If I recall my freshman discrete maths, logical conjunction (and)
was the multiplicative operation in boolean algebra, logical disjunction
(or) was the additive operation. Or at least were denoted by the same
symbols and the analogy made it easier to remember the meaning of those
symbols than â?§ and â?¨. And in arithmetical maths expressions,
multiplication binds before addition. So I might have jumped to the
conclusion that in boolean expressions, and would bind before or. (Or
made a really lucky guess as to the correct convention.)
Either way, operator precedence is a convention, not a rule - just a
very widespread convention. And speaking of logic design, that I
remember to really have beastly logical expressions in it. And wonder of
wonders, I also remember that terms using one operator were always
parenthesized.
was the multiplicative operation in boolean algebra, logical disjunction
(or) was the additive operation. Or at least were denoted by the same
symbols and the analogy made it easier to remember the meaning of those
symbols than â?§ and â?¨. And in arithmetical maths expressions,
multiplication binds before addition. So I might have jumped to the
conclusion that in boolean expressions, and would bind before or. (Or
made a really lucky guess as to the correct convention.)
Your memory is correct; Boolean + is OR, and Boolean * is AND. (You can
remember by observing that this makes the tables the same as arithmetic
in seven of the eight possible cases.) This is the reason that AND binds
more tightly than OR in nearly all languages.
(The designers of Ada, however, decided that not enough programmers are
familiar with Boolean Algebra, and defined the syntax in such a way that
AND and OR cannot be mixed in a single expression without explicit
parentheses.)