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

Discussion in 'Perl Misc' started by kramer, Oct 9, 2008.

  1. kramer

    kramer Guest

    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?
     
    kramer, Oct 9, 2008
    #1
    1. Advertising

  2. kramer <> wrote:
    > 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").



    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Oct 9, 2008
    #2
    1. Advertising

  3. kramer

    Jim Gibson Guest

    In article <gcjig4$fg3a$>, kramer <>
    wrote:

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

    --
    Jim Gibson
     
    Jim Gibson, Oct 9, 2008
    #3
  4. kramer

    Ben Morrow Guest

    Quoth Jim Gibson <>:
    > In article <gcjig4$fg3a$>, kramer <>
    > wrote:
    >
    > > 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));'.


    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

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
     
    Ben Morrow, Oct 9, 2008
    #4
  5. kramer <> wrote:
    >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
     
    Jürgen Exner, Oct 9, 2008
    #5
  6. kramer

    Willem Guest

    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
     
    Willem, Oct 9, 2008
    #6
  7. kramer

    kramer Guest

    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

    Ben Morrow wrote:
    > Quoth Jim Gibson <>:
    >> In article <gcjig4$fg3a$>, kramer <>
    >> wrote:
    >>
    >>> 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));'.

    >
    > 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
    >
     
    kramer, Oct 9, 2008
    #7
  8. Willem <> wrote:
    >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ürgen Exner, Oct 9, 2008
    #8
  9. Jim Gibson wrote:
    > In article <gcjig4$fg3a$>, kramer <>
    > wrote:
    >
    >> 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));'.


    COBOL


    --
    John W. Kennedy
    A proud member of the reality-based community.
     
    John W Kennedy, Oct 9, 2008
    #9
  10. Joost Diepenmaat, Oct 9, 2008
    #10
  11. kramer

    Guest

    On Thu, 09 Oct 2008 11:29:15 -0400, kramer <> wrote:

    >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
    >
    >Ben Morrow wrote:
    >> Quoth Jim Gibson <>:
    >>> In article <gcjig4$fg3a$>, kramer <>
    >>> wrote:
    >>>
    >>>> 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));'.

    >>
    >> 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
    >>


    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.
     
    , Oct 9, 2008
    #11
  12. kramer

    Todd Wade Guest

    On Oct 8, 8:16 pm, kramer <> wrote:
    > 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.
     
    Todd Wade, Oct 10, 2008
    #12
  13. On 2008-10-09 02:32, Jürgen Exner <> wrote:
    > kramer <> wrote:
    >>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


    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
     
    Peter J. Holzer, Oct 11, 2008
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Hubbard
    Replies:
    2
    Views:
    863
    David Hubbard
    Jan 12, 2006
  2. Shawn
    Replies:
    2
    Views:
    391
    =?Utf-8?B?RWx0b24gVw==?=
    Feb 19, 2006
  3. =?Utf-8?B?UHVuaXNoZXI=?=

    Need same viewstate for same page across multiple servers

    =?Utf-8?B?UHVuaXNoZXI=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    439
    John Timney \( MVP \)
    Mar 23, 2006
  4. darrel
    Replies:
    2
    Views:
    492
    darrel
    Apr 5, 2006
  5. Krick
    Replies:
    1
    Views:
    1,534
    John C. Bollinger
    Aug 15, 2003
Loading...

Share This Page