[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

and and or priority

Pavel Smerk

8/25/2006 5:27:00 PM

OMG!

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?

(Yes, long time spent on that now, who would expect such a curiosity...)

Thanks for explanation, if there is any,

P.
17 Answers

Justin Collins

8/25/2006 6:30:00 PM

0

Pavel Smerk wrote:
> OMG!
>
> 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?
>
> (Yes, long time spent on that now, who would expect such a curiosity...)
>
> Thanks for explanation, if there is any,
>
> P.
>

To keep you on your toes...okay, I don't actually know.

-Justin

Alex Young

8/25/2006 7:39:00 PM

0

Pavel Smerk wrote:
> OMG!
>
> 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...

Have you got a counterexample handy?

--
Alex

Elliot Temple

8/25/2006 7:51:00 PM

0


On Aug 25, 2006, at 12:38 PM, Alex Young wrote:

> Pavel Smerk wrote:
>> OMG!
>> 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...
>
> Have you got a counterexample handy?

irb(main):008:0> true or true and false
=> false

If "and" had higher priority, that'd evaluate as:

true or true and false
true or false
true

I think it's going left to right:

true or true and false
true and false
false

Pickaxe page 339 says they have the same priority and calls them
"logical composition" operators.

As to the OP's question: I don't know why.

-- Elliot Temple
http://www.cur...




Alex Young

8/25/2006 8:21:00 PM

0

Elliot Temple wrote:
>
> On Aug 25, 2006, at 12:38 PM, Alex Young wrote:
>
>> Pavel Smerk wrote:
>>> OMG!
>>> 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...
>>
>> Have you got a counterexample handy?
>
> irb(main):008:0> true or true and false
> => false
>
Good counter-example :-) Now I understand. It seems to be evaluated in
terms of the first encountered. Unless... that's wrong too? :-)

--
Alex

David Vallner

8/25/2006 8:40:00 PM

0

Pavel Smerk wrote:
> OMG!
>
> 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?
>
> (Yes, long time spent on that now, who would expect such a curiosity...)
>
> Thanks for explanation, if there is any,
>
> P.
>

<rant>
Just. Use. Parentheses. Even in programming languages where the rules
are clear. Explicit >> Implicit.
</rant>

David Vallner

Elliot Temple

8/25/2006 9:00:00 PM

0


On Aug 25, 2006, at 1:40 PM, David Vallner wrote:

> Explicit >> Implicit.

STDOUT.puts "i disagree"

-- Elliot Temple
http://www.cur...




Pavel Smerk

8/26/2006 7:49:00 AM

0

David Vallner wrote:
> Pavel Smerk wrote:
>
>> OMG!
>>
>> 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?
>>
>> (Yes, long time spent on that now, who would expect such a curiosity...)
>>
>> Thanks for explanation, if there is any,
>>
>> P.
>>
>
> <rant>
> Just. Use. Parentheses. Even in programming languages where the rules
> are clear. Explicit >> Implicit.
> </rant>

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. ;-)

David Vallner

8/26/2006 3:16:00 PM

0

Pavel Smerk wrote:
> David Vallner wrote:
>> Pavel Smerk wrote:
>>
>>> OMG!
>>>
>>> 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?
>>>
>>> (Yes, long time spent on that now, who would expect such a curiosity...)
>>>
>>> Thanks for explanation, if there is any,
>>>
>>> P.
>>>
>>
>> <rant>
>> Just. Use. Parentheses. Even in programming languages where the rules
>> are clear. Explicit >> Implicit.
>> </rant>
>
> 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.

David Vallner

Elliot Temple

8/26/2006 4:13:00 PM

0


On Aug 26, 2006, at 8:16 AM, David Vallner wrote:

> I agree that the and should bind more tightly than or now that
> that's been brought up.

&& binds before ||

Presumably there is a special reason for and/or to be the way they
are. We should probably find out what it is before suggesting it be
changed.

irb(main):025:0> true or true and false
=> false
irb(main):026:0> true || true && false
=> true


-- Elliot Temple
http://www.cur...




Matthew Smillie

8/27/2006 2:31:00 AM

0

On Aug 26, 2006, at 16:16, David Vallner wrote:

> Pavel Smerk wrote:
>> David Vallner wrote:
>>> Pavel Smerk wrote:
>>>
>>>> OMG!
>>>>
>>>> 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?
>>>>
>>>> (Yes, long time spent on that now, who would expect such a
>>>> curiosity...)
>>>>
>>>> Thanks for explanation, if there is any,
>>>>
>>>> P.
>>>>
>
> 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.

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.

matthew smillie.