Perl: the Markov chain saw PerlMonks

### Re^5: One out of three ain't bad (order)

by Tanktalus (Canon)
 on Oct 23, 2005 at 15:39 UTC ( #502316=note: print w/replies, xml ) Need Help??

I'm using some because the parent used sum. And s/he's using sum because s/he is emulating tye's addition of booleans.

You're right that !! screams boolean. However, that's not what is happening here. We are adding numbers here. Let me ask you what "true + false + true" is. That's just nonsensical. Programmers have been getting away with it for years because computers only deal with numbers - whether that's boolean, character, or, oddly enough, numerical. But, abstractly, it makes no sense. You can't add booleans. Boolean algebra consists of operators such as not, and, or, xor, nand, and nor. (I may have missed some - it's been a few years since I've dealt that directly with boolean algebra in the theoretical sense.) Addition, subtraction, multiplication, division - all of these are nonsensical in the boolean world. In fact, the multiplication symbol (the middle-dot, ·) was reused for the "and" operator, and the addition symbol (+) was reused for the "or" operator, IIRC (which I may not recall correctly...). And, as in mathematical algebra, the multiplication symbol is optional between terms, further reusing the sigils.

Therefore, adding boolean values is just confusing. The answer to the question above is that "true + false + true" equals "true" (numerically: 1 || 0 || 1) - and the OP wants this to be a count of 2. Which, if my memory serves, the OP wants "a nor b nor c" Unfortunately, the nor solution doesn't scale past three.

grep, unlike sum, uses boolean context. So the choices are to either sum numbers (using the ternary operator, for example), or to grep a list in scalar context. Adding booleans makes me question whether the author knows what s/he's doing, or is just copying what someone else is doing - whether s/he's too clever (in taking advantage of undefined behaviour) or not clever enough by half (in just copying something that works without understanding that it's undefined behaviour).

• Comment on Re^5: One out of three ain't bad (order)

Replies are listed 'Best First'.
Re^6: One out of three ain't bad (order)
by eric256 (Parson) on Oct 24, 2005 at 02:08 UTC

Hehe. True enough and i didn't even see that before you posted. Thanks for clarifying. I should know better after discovering that different versions of Access use differn't values for true and false one uses 1 for true, while another uses -1.. which causes huge confusion when relying on 1 ....which as you point out I shouldn't.

/me sets back in his chair and waits for perl6 so i can do if one(@a) == true { }

___________
Eric Hodges
Re^6: One out of three ain't bad (order)
by sauoq (Abbot) on Oct 26, 2005 at 04:39 UTC
In fact, the multiplication symbol (the middle-dot, ·) was reused for the "and" operator, and the addition symbol (+) was reused for the "or" operator, IIRC (which I may not recall correctly...).

You recall correctly. Some people find it counterintuitive that 'plus' (+) should be used for 'or' because, in natural language, we often use 'plus' as an informal conjunction meaning 'and'. But... it makes perfect sense when you compare the boolean algebra operators with their counterparts in arithmetic...

 A B A and B A times B 0 0 0 0 0 1 0 0 1 0 0 0 1 1 1 1

 A B A or B A plus B 0 0 0 0 0 1 1 1 1 0 1 1 1 1 1 2

So, addition is essentially boolean disjunction with the added feature of counting how many clauses are true. Having more than two values comes in handy. (Note that xor is the same as addition modulus 2.) The OP's query can be answered using only standard boolean algebra operators, of course. \$x xor \$y xor \$z and not (\$x and \$y) would be one way.

```-sauoq
"My two cents aren't worth a dime.";
```

Create A New User
Node Status?
node history
Node Type: note [id://502316]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-04-23 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (84 votes). Check out past polls.

Notices?