http://qs321.pair.com?node_id=394269

A couple of nights ago, I was thinking a little, about Perl, about programming in general, about operators (especially "and" and "or" (or their higher precedence brothers "&&" and "||")) and I thought I would like a "but" operator. I told that to my girlfriend.

Well, she's no programmer but she does understand a programmer's way of thinking and she has some understanding of boolean logic.

So I told her I would like a "but" operator. Then she said, "that wouldn't be quite usefull, because that would make you able to say (pseudocode)  not EXPR-THIS but EXPR-THAT or  EXPR-THAT but not EXPR-THIS and in that case, you could as well just say  EXPR-THAT."

Hmm, I thought. You are pretty right, actually. Then I discoverd that the importing mechanism does have something like a "but" operator (use MyModule qw(:standard !somesub)). But (<-- there it is) I didn't enjoy that one as much as a true "but" operator that would work a little like "and" and "or".

A long story made short: I (er... my girlfriend) concluded a "but" operator would be useless and I think it's a pity. I still like the abstract idea of having such a thing.

Whatever.




"2b"||!"2b";$$_="the question"
Besides that, my code is untested unless stated otherwise.
One more: please review the article about regular expressions (do's and don'ts) I'm working on.

Replies are listed 'Best First'.
Re: A "but" operator.
by theorbtwo (Prior) on Sep 27, 2004 at 18:09 UTC

    It's quite simple, really. "but" is just like "and" when used as a conjunction. In natural language, "and" has the conotation that the two things are normally in agreement, or neutral toward each-other, whereas "but" has the conotation that the two things are normally in oppisition. Note that people very rarely make "but" lists, but quite often make "and" or "or" lists; "but" is a preposition only, but "and" and "or" are list specifiers.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      Maybe I don't get you right, maybe you're wrong. I'm not sure. The way I get it, you say "but" is just like "and". How's that possible? I could say: "you have got brown hair AND I have got black hair" (this indicates both of them are true). I could say: "you have got brown hair OR I have got black hair" (one of them would be true, but it is not specified which one [update that would be XOR. At least one of them is true, in a programmer's view]). I could also say: (oh, I think I do understand you now) "you have got brown hare BUT I have got black hair" (again, both would be true). Ok.

      Hmm, nice stuff to think about :)




      "2b"||!"2b";$$_="the question"
      Besides that, my code is untested unless stated otherwise.
      One more: please review the article about regular expressions (do's and don'ts) I'm working on.
        you say "but" is just like "and". How's that possible?

        In fact, some languages have words that can be translated into English as either "but" or "and". For example, the Greek postpositive "de" (delta epsilon) is most frequently translated into English as "but", but it can also be translated into English as "and" (though for "and" the conjunction "kai" is much more common).</philolophile>

        The meaning of "but" and "and" in English _is_ different, but the difference doesn't have very much to do with the meaning of "and" in Perl. The real difference is that "but" implies that the second item may be surprising given the first, while "and" has only the more basic meaning of conjunction. Still, in terms of their value as boolean operators, both would mean the same thing: the first argument is true; the second argument is also true. Boolean logic doesn't much care about surprise value. Think about the difference in meaning between "The ball is blue and it is also heavy" versus "The ball is blue but it is also heavy". In both cases we're dealing with a heavy blue ball, but in the latter case there is an implication (quite a surreal one, given the usual presumption that color and weight are pretty much orthogonal) that we might ordinarily expect a blue ball not to be heavy -- but this one is.

        And, as pointed out, the word "but" is going to be used in Perl6 for something really cool that will be sure not to disappoint you, though not as a boolean operator:

        my $x = ("The answer to the ultimate question of life, the universe +, and everything" but 42 but false but undef); print "Number: " . (0+$x) . "\n"; print "String: " . $x . "\n"; print "Boolean: false\n" unless $x; print "Undefined" if not defined $x;

        (I might have some legacy Perl5 syntax in there by mistake. For some reason, though I want to learn and use Perl6, I keep finding myself being lured away by Perl5 and its persistent siren call of actually being ready for use now.)


        "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
        "A but B" <=> "A and !B". Which, scarily enough, is equivalent to "!(A -> B)" (A implies B, or "if A, then B"). Which, if you think and squint, makes a weird kind of sense.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

        In your analogy, but can't be XOR, it's ($A and !$B). XOR is ($A and !$B) || (!$A and $B). Or equivalently, but could be "A and (A XOR B)", if you prefer to keep it complicated and silly, in a Lewis Caroll (sp?) sort of way.
      The English "but" is a little hard for me to pin down in my mind. Perhaps it has too many meanings to make a good programming word.
    • I had brown hair, but now it's gray. (both are true)
    • He wanted to go, but had other commitments. (condition modified by 'but phrase')
    • She had but one laptop. (some kind of exclusive emphasis?)
    • -Theo-
      (so many nodes and so little time ... )

        The first meaning could actually be useful in programming to indicate that a variable should have a different value under certain conditions, but with the ability to remember the past condition and revert to it automatically.

        Then you could do something like this:

        my $var1 = "foo" but ($some_input_var eq 'show_bar') { "bar" }; print $var1; # prints 'foo' #$some_input_var is changed by a user to 'show_bar'; print $var1; #prints 'bar' #$some_input_var is changed to something else print $var1; #prints 'foo'

        Of course after thinking about it, its totally pointless ... one could do the same thing with

        $var1 = sub { 'foo' ? $some_input_var ne 'show_bar' : 'bar' };
Re: A "but" operator.
by BrowserUk (Patriarch) on Sep 27, 2004 at 18:35 UTC

    P6 is destined to have a proctologist, though I don't think it has quite the meaning you are looking for.

    The example that sticks in my brain is

    my $x = 0 but true;

    I know I run across other examples in the A|S|Enn, but their significance has been lost in the melee of other new stuff in the same examples. It'll probably click eventually.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      The but operator in Perl 6 is really a mixin operator underneath. Or looking at it the other way around, mixins already have a strong component of "I wish to contradict something someone said earlier".

        Something like?

        my Model $T = Ford( color => any( @colors ) ) but 'black';

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: A "but" operator.
by SpanishInquisition (Pilgrim) on Sep 27, 2004 at 19:00 UTC

    "But" is not a conditional operator like "and", it's actually two seperate statements with an implied mutual exclusivity between them. What "but" would return in scalar or array context is up for debate, but it's probably something like:

    sub but() { my ($x,$y) = @_; die "that's unpossible!" if (!$x && $y) return $x; }

    Interesting thread, but I agree with your girlfriend ... it's totally useless :)

Re: A "but" operator.
by PodMaster (Abbot) on Sep 28, 2004 at 05:32 UTC
    When it comes to boolean logic (truth tables, etc), "but" is "and". The only difference is one of emphasis/constrast (what theorbtwo was talking about). For example
    1) I went to school and bob went to work. 2) I went to school but bob went to work.
    The two statements are logically equivalent, but in #2 the writer wishes to emphasize the difference.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: A "but" operator.
by ambrus (Abbot) on Sep 27, 2004 at 18:49 UTC

    TriIntercal has a but operator, written as a @ sign. It does not do what you were talking about, though.

Re: A "but" operator.
by zentara (Archbishop) on Sep 28, 2004 at 14:11 UTC
    When I was thinking about how to develop an Artificial Intelligence program, I realized that boolean logic is good for computer circuits, but the real world needs a "3 state logic" that has a "time component" I will call "maybe".

    So to think like a human, your logic descisions would be one of "yes no maybe". The "maybe" traverses time, because it means "will be determined later". Now the problem becomes how to keep track of all the "maybe's" and go back and redo all the previous logic once a certain unknown becomes known.

    It seems that your "but" operator, is similar to my "maybe", it's a descision defered to a later time.

    P.S. At first I thought this was a joke, you telling your girlfriend you want some but. :-)


    I'm not really a human, but I play one on earth. flash japh
Re: A "but" operator.
by halley (Prior) on Sep 28, 2004 at 16:02 UTC
    My mother refuses to touch computers. She's a secretary by trade, and still works on an IBM Selectric II typewriter, rather than do any word processing. She says it's because her eyes don't get along with CRTs, but some of it is just stubbornness.

    But she's always had the technical and literal mindset which I find in computer geekdom. She conceptually invented the video tape recorder before they were available or even known to the public. She had to explain the concept of floppy disks and files to a guy who had just bought a Smith Corona word processor.

    As far back as I can remember, my mom would playfully answer boolean questions in a boolean sense. "Do you want orange juice or apple juice?" "Yes." "Do you vote Republican or Democrat?" "Yes." She would always grin at this point, and the questioner was left to stare blankly or rephrase their question. She'd also rattle off phrases with many carefully counted negatives, "I don't want none of your not misbehaving nor being contrary," leaving us to unravel the intent of the sentence, to see if it matched by context what she really meant.

    She loved all manner of word games, but I'm sure the logical word games alone are the key formative experience that led me to my current career. It didn't make choices for me, but shaped my thoughts and affinities to the point where computer programming was an obvious pursuit.

    --
    [ e d @ h a l l e y . c c ]

      While travelling, I met a Japanese guy who answered just like that - "Tea or coffee?" - "Yes". To him, it seemed to be natural. Either that's his personal way of thinking, the Japanese culture, or his understanding of this kind of questions in a foreign language (English). Everybody who met the guy for the first time was quite surprised by his answers, and he didn't understand why.

      Back on topic: as 'but' seems to have so many different meanings, I doubt its introduction would make the language Perl clearer in any way. For me as a German speaker, I first think of 'but' indicating some sort of contradiction or distinction. 'But' in the sense of 'and' is also quite common, but that would make no difference to the existing operator 'and'. So I still don't really dig the possible benefits of a 'but' operator, and I'd most probably never use it.

Re: A "but" operator.
by astroboy (Chaplain) on Oct 01, 2004 at 08:36 UTC
    "Butt Operator" -- sounds like a great name for a porno flick