($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???

K

kramer

I expected ($x == ($y or $z)) to return true if $x equals either $y or
$z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.

Why?
 
T

Tad J McClellan

kramer said:
I expected ($x == ($y or $z)) to return true if $x equals either $y or
$z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.

Why?


Because you told it (with parenthesis) to evaluate the "or"
before evaluating the "==".

And if you stop telling it that, ie:

($x == $y or $z)

Then it will evaluate the "$x == $y" followed by evaluating the "or"
(with one of its operands being the boolean result of the "$x == $y").
 
J

Jim Gibson

kramer <[email protected]> said:
I expected ($x == ($y or $z)) to return true if $x equals either $y or
$z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.

Why?

Because Perl is not English. I can't think of any computer language
that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.

All computer languages with which I am familiar will respect the
explicit parentheses you have added to that expression and evaluate
'($y or $z)' first, then evaluate the result of that sub-expression in
'($x == result)'.

Perhaps you were thinking of arithmetic:

($x * ($y + $z)) == (($x * $y) + ($x * $z))

or maybe boolean logic:

($x and ($y or $z)) == (($x and $y) or ($x and $z))
 
B

Ben Morrow

Quoth Jim Gibson said:
Because Perl is not English. I can't think of any computer language
that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.

use Quantum::Superpositions;

my ($x, $y, $z) = (1, 2, 1);

if ($x == any($y, $z)) {
print "YAY!";
}

This will be built in to Perl 6, where (IIRC) you will be able to write

($x == $y|$z)

The concept is called 'junctions'.

Ben
 
J

Jürgen Exner

kramer said:
I expected ($x == ($y or $z)) to return true if $x equals either $y or
$z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.
Why?

Because even for Boolean values '==' and 'or' are not distributive, let
alone for non-Boolean values which you are probably using.

1 == (0 or 0) ====> 1 == 0 ====> 0
(1 or 0) == (1 or 0) ====> 1 == 1 ====> 1

jue
 
W

Willem

Jim Gibson wrote:
) Because Perl is not English. I can't think of any computer language
) that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.

Perl 6 perhaps ? :)


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
K

kramer

Thanks Ben. This is the kind of answer I was looking for. After 40
years of programming on other languages and now picking up perl I am
constantly surprised at how often perl somehow correctly "interprets" my
"english" intents and gives me what I want. But not so in this case so
I asked.

Even though it looks like it would break many other things are
"junctions" one of the per6 items I can somehow use as a feature in
5.010 now?

Your example triggered smart matches so I'll have to check to see if
that can be bent to get what I want. All just to save a bunch of
repetitive typing!

Jack
 
J

Jürgen Exner

Willem said:
Jim Gibson wrote:
) Because Perl is not English. I can't think of any computer language
) that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.

Perl 6 perhaps ? :)

I sure hope not because it would be mathematically wrong. As I mentioned
in a previous answer '==' and 'or' are _NOT_ distributive.

Maybe using a different notation, I have no problem with that. But for
heavens sake not for '==' and 'or'. Their semantics have been fixed a
long, long time ago.

jue
 
J

John W Kennedy

Jim said:
Because Perl is not English. I can't think of any computer language
that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.

COBOL
 
S

sln

Thanks Ben. This is the kind of answer I was looking for. After 40
years of programming on other languages and now picking up perl I am
constantly surprised at how often perl somehow correctly "interprets" my
"english" intents and gives me what I want. But not so in this case so
I asked.

Even though it looks like it would break many other things are
"junctions" one of the per6 items I can somehow use as a feature in
5.010 now?

Your example triggered smart matches so I'll have to check to see if
that can be bent to get what I want. All just to save a bunch of
repetitive typing!

Jack

Re: x == $y|$z
This is a hard one to chomp. In C++ if x were an object, all operators could
be overloaded, ie: == to take the rvalue. If y overloaded the |, it could return
an array, which gets passed to x (because x is looking for an array passed in with
its overloaded == function). But these are objects in C++, intrinsic types will obey
language. If the language is built on C++ then anything is possible.


Re: $x == any($y, $z)
This is a puzzle, a Perl guts thing.
 
T

Todd Wade

I expected ($x == ($y or $z)) to return true if $x equals either $y or
$z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.

Why?

Here is how I would do it in stock perl:

grep $x == $_, $y, $z

As in:

if ( grep { $x == $_ } $y, $z ) {
print 'x is the same as y or z';
}

Todd W.
 
P

Peter J. Holzer

Because even for Boolean values '==' and 'or' are not distributive, let
alone for non-Boolean values which you are probably using.

1 == (0 or 0) ====> 1 == 0 ====> 0
(1 or 0) == (1 or 0) ====> 1 == 1 ====> 1

He wants to distribute the == over the or, not vice versa, so that
would be:

(1 == 0) or (1 == 0) ====> 0 or 0 ====> 0

works ;-).

Of course it doesn't work in the general case. E.g.,

0 == (0 or 1) ====> 0 == 1 ====> 0
(0 == 0) or (0 == 1) ====> 1 or 0 ====> 1

more specifically, since ($y or $z) returns $y if $y is not false, and
otherwise $z, it works if $x has a true value.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top