Symbolic representation of logical operators

Symbolic representation of logical operators

am 19.08.2007 17:08:11 von markhobley

Some logical operators have symbolic representation. For example:

The doubleampersand && represents the "and" operator
The doublepipe || represents the "or" operator
The pling ! represents the "not" operator

What is the symbolic representation for the "xor" operator?
I don't appear to be able to find this in any documentation.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Re: Symbolic representation of logical operators

am 19.08.2007 17:14:34 von jurgenex

Mark Hobley wrote:
> Some logical operators have symbolic representation. For example:
>
> The doubleampersand && represents the "and" operator
> The doublepipe || represents the "or" operator
> The pling ! represents the "not" operator

No, those are actually different operators because e.g. "and" and "&&" have
different precedences.

jue

Re: Symbolic representation of logical operators

am 19.08.2007 17:26:38 von Josef Moellers

Mark Hobley wrote:
> Some logical operators have symbolic representation. For example:
>
> The doubleampersand && represents the "and" operator
> The doublepipe || represents the "or" operator
> The pling ! represents the "not" operator
>
> What is the symbolic representation for the "xor" operator?
> I don't appear to be able to find this in any documentation.

The caret "^".

BTW as Jürgen has pointed out, "&&" is not a bitwise and but rather a
logical and and it short-circuits, likewise "||" is a logical or:

sub l1 {
print "l1\n"; return 1;
}
sub l2 {
print "l2\n"; return 0;
}
print l2 && l1, "\n";
print l1 || l2, "\n";

You want "&" and "|".

Josef
--
Mails please to josef dot moellers
and I'm on gmx dot de.

Re: Symbolic representation of logical operators

am 19.08.2007 19:40:23 von Michele Dondi

On Sun, 19 Aug 2007 17:26:38 +0200, Josef Moellers
<5502109103600001@t-online.de> wrote:

>> Some logical operators have symbolic representation. For example:
>>
>> The doubleampersand && represents the "and" operator
>> The doublepipe || represents the "or" operator
>> The pling ! represents the "not" operator
>>
>> What is the symbolic representation for the "xor" operator?
>> I don't appear to be able to find this in any documentation.
>
>The caret "^".

Except that it is a bitwise operator.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Symbolic representation of logical operators

am 19.08.2007 20:08:07 von markhobley

Josef Moellers <5502109103600001@t-online.de> wrote:
>
> The caret "^".
>
> BTW as Jürgen has pointed out, "&&" is not a bitwise and but rather a
> logical and and it short-circuits, likewise "||" is a logical or:

I am not looking for a bitwise operator, I am looking for a the
symbolic form of a logical exclusive or to compliment the && and || operators,

The logic being as follows:

false (symbol) false = false
false (symbol) true = true
true (symbol) false = true
true (symbol) true = false (a conventional || or would return true here)

I hope that makes sense.

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Re: Symbolic representation of logical operators

am 19.08.2007 21:31:06 von Joe Smith

Mark Hobley wrote:
> Some logical operators have symbolic representation. For example:
>
> The doubleampersand && represents the "and" operator
> The doublepipe || represents the "or" operator
> The pling ! represents the "not" operator
>
> What is the symbolic representation for the "xor" operator?
> I don't appear to be able to find this in any documentation.

No, "&&" does not represent the "and" operator. Those are two
different operators - they have different precedences. If you
replace one for the other in a program you will often get different
results; they are not interchangeable.

The answer to your question is that there is none.
-Joe

Re: Symbolic representation of logical operators

am 19.08.2007 22:03:16 von Josef Moellers

Mark Hobley wrote:
> Josef Moellers <5502109103600001@t-online.de> wrote:
>> The caret "^".
>>
>> BTW as Jürgen has pointed out, "&&" is not a bitwise and but rather a
>> logical and and it short-circuits, likewise "||" is a logical or:
>
> I am not looking for a bitwise operator, I am looking for a the
> symbolic form of a logical exclusive or to compliment the && and || operators,
>
> The logic being as follows:
>
> false (symbol) false = false
> false (symbol) true = true
> true (symbol) false = true
> true (symbol) true = false (a conventional || or would return true here)
>
> I hope that makes sense.

OK, my fault.

How about "!="?

for my $v1 (0,1) {
for my $v2 (0,1) {
print "$v1 != $v2 = ", $v1 != $v2, "\n";
}
}

Josef
--
Mails please to josef dot moellers
and I'm on gmx dot de.

Re: Symbolic representation of logical operators

am 19.08.2007 22:08:11 von markhobley

Mark Hobley wrote:
> I am not looking for a bitwise operator, I am looking for a the
> symbolic form of a logical exclusive or to compliment the && and || operators,

print (2 && 3); # 3, Ok, I was expecting 1, but checked documentation
print (0 && 2); # 0, Ok
print (2 || 3); # 3, Ok, again documented
print (0 || 2); # 2, Ok documented
print (2 ^^ 3); # Syntax error, I hoped 0 (true xor true = false)
print (7 ^ 2); # 5, Ok, but that is a bitwise operator. I am testing logicals
print (7 xor 2); # Empty string, I was expecting 0 (true xor true = false)

I wondered why the xor gives an empty string, and not the second values like
the && and ||, so I thought maybe its because its a named operator, so I tried:

print (0 or 0); # 0, Ok
print (0 and 0); # 0, Ok
print (0 xor 0); # Empty string, why not a 0, like and and or

I know the empty strings and non zero values are fine for logicals, I just
didn't expect some of the results.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Re: Symbolic representation of logical operators

am 19.08.2007 22:57:32 von Michele Dondi

On Sun, 19 Aug 2007 20:08:11 GMT, markhobley@hotpop.deletethisbit.com
(Mark Hobley) wrote:

>I know the empty strings and non zero values are fine for logicals, I just
>didn't expect some of the results.

Rationale is that Perl tries to be dwimmy, and it generally succeeds.
Of course it can't get it right 100% of the times for 100% of the
people.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Symbolic representation of logical operators

am 20.08.2007 00:08:02 von markhobley

Josef Moellers <5502109103600001@t-online.de> wrote:

> How about "!="?

It doesn't work for two differing non-zero values:

2 != 3 ; true (an xor would be false here, because both 2 and 3 are true)

It looks like I have to use the named xor and parenthesis.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Re: Symbolic representation of logical operators

am 20.08.2007 00:21:22 von xhoster

markhobley@hotpop.deletethisbit.com (Mark Hobley) wrote:
> Mark Hobley wrote:
> > I am not looking for a bitwise operator, I am looking for a the
> > symbolic form of a logical exclusive or to compliment the && and ||
> > operators,
>
> print (2 && 3); # 3, Ok, I was expecting 1, but checked documentation
> print (0 && 2); # 0, Ok
> print (2 || 3); # 3, Ok, again documented
> print (0 || 2); # 2, Ok documented
> print (2 ^^ 3); # Syntax error, I hoped 0 (true xor true = false)
> print (7 ^ 2); # 5, Ok, but that is a bitwise operator. I am testing
> logicals print (7 xor 2); # Empty string, I was expecting 0 (true xor
> true = false)
>
> I wondered why the xor gives an empty string, and not the second values
> like the && and ||,

||, &&, "and", and "or" all return the value of the last argument that
||needed
to be inspected in order to know what the truth-value of the expression is.
This makes sense, as the boolean value of the last expression it needs to
inspect is always identical to the boolean value of the expression as a
whole, and so returning it gives a correct answer and always much nice code
streamlining. For "xor", this is not the case. It always has to inspect
both arguments, and sometimes it has to yield a value which does not have
the same boolean value as either of its arguments does. Therefore it
creates its result value de novo, rather than just grabbing one of the
arguments.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

Re: Symbolic representation of logical operators

am 20.08.2007 00:22:30 von Tad McClellan

Mark Hobley wrote:

> print (2 && 3); # 3, Ok, I was expecting 1, but checked documentation
> print (2 || 3); # 3, Ok, again documented

> print (7 xor 2); # Empty string, I was expecting 0 (true xor true = false)


You should have instead been expecting any of Perl's false values.


> I wondered why the xor gives an empty string, and not the second values like
> the && and ||,


Because xor cannot short-circuit, just as its documentation says.


> print (0 xor 0); # Empty string, why not a 0, like and and or


&& and || do not return zero, they return the last expression evaluated
(they "short circuit"). No zero here:


perl -we 'print undef or 0'


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Symbolic representation of logical operators

am 20.08.2007 03:40:47 von rvtol+news

Mark Hobley schreef:

> I am looking for a the symbolic form of
> a logical exclusive or to compliment the && and ||
> operators,
>
> The logic being as follows:
>
> false (symbol) false = false
> false (symbol) true = true
> true (symbol) false = true
> true (symbol) true = false

I guess you could use:

!!$v1 ^ !!$v2


For example:

$ perl -wle'
$v1 = "";
$v2 = "0E0";
print !!$v1 ^ !!$v2;
'
1

--
Affijn, Ruud

"Gewoon is een tijger."

Re: Symbolic representation of logical operators

am 20.08.2007 12:23:13 von QoS

markhobley@hotpop.deletethisbit.com (Mark Hobley) wrote in message-id: <1l1mp4-ifu.ln1@neptune.markhobley.yi.org>

>
> Some logical operators have symbolic representation. For example:
>
> The doubleampersand && represents the "and" operator
> The doublepipe || represents the "or" operator
> The pling ! represents the "not" operator
>
> What is the symbolic representation for the "xor" operator?
> I don't appear to be able to find this in any documentation.
>
> Regards,
>
> Mark.
>
> --
> Mark Hobley
> 393 Quinton Road West
> QUINTON
> Birmingham
> B32 1QE
>
> Email: markhobley at hotpop dot donottypethisbit com
>
> http://markhobley.yi.org/

#nand gates allow the construction of any of the logical operators.
my $a = 1;
my $b = 1;
my ($o1, $o2, $o3, $o4,);

#o4 is the result of $a xor $b;
$o1 = $a !& $b;
$o2 = $a !& $o1;
$o3 = $b !& $o1;
$o4 = $o2 !& $o3;

Re: Symbolic representation of logical operators

am 20.08.2007 18:59:48 von merlyn

>>>>> "Ruud" == Ruud writes:

Ruud> I guess you could use:

Ruud> !!$v1 ^ !!$v2

Or more simply:

$v1 ? !$v2 : $v2

This will have the proper truth-iness, although there's no easy way to
preserve "last expression evaluated" as with the other short-circuit
operators.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
Posted via a free Usenet account from http://www.teranews.com

Re: Symbolic representation of logical operators

am 20.08.2007 22:31:13 von anno4000

Randal L. Schwartz wrote in comp.lang.perl.misc:
> >>>>> "Ruud" == Ruud writes:
>
> Ruud> I guess you could use:
>
> Ruud> !!$v1 ^ !!$v2
>
> Or more simply:
>
> $v1 ? !$v2 : $v2
>
> This will have the proper truth-iness, although there's no easy way to
> preserve "last expression evaluated" as with the other short-circuit
> operators.

Well, you can't expect too much. If the result of the xor is true,
that's fine: there's exactly one true argument, which should be
returned. If the result is false, if we're lucky both arguments
are false, in which case either one can be returned, though there
is no canonical way to choose. If both arguments are true, none
of them can be used, so an "artificial" false has to be returned.

!$v1 ? $v2 :
!$v2 ? $v1 :
!1;

That should be as close as it gets. It arbitrarily returns $v2 in
the false-false case. The final "!1" might as well have been "0",
but this way it returns the Perl-specific Janus-faced false.

Anno

Re: Symbolic representation of logical operators

am 21.08.2007 02:38:54 von Bart Lateur

Mark Hobley wrote:

>I am not looking for a bitwise operator, I am looking for a the
>symbolic form of a logical exclusive or to compliment the && and || operators,

There is none. You might have guessed that by now.

The logical choice would have been the double caret.

--
Bart.

Re: Symbolic representation of logical operators

am 21.08.2007 12:08:03 von markhobley

Mark Hobley wrote:

> I know the empty strings and non zero values are fine for logicals, I just
> didn't expect some of the results.

I found some more apparent weirdness:

print ("apples" || "pears"); # apples
print ("apples" && "pears"); # pears

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Re: Symbolic representation of logical operators

am 21.08.2007 12:23:56 von Paul Lalli

On Aug 21, 6:08 am, markhob...@hotpop.deletethisbit.com (Mark Hobley)
wrote:

> I found some more apparent weirdness:
>
> print ("apples" || "pears"); # apples
> print ("apples" && "pears"); # pears


Why are you calling this "weirdness"? As has already been explained,
the logical operators return the last value they have to evaluate to
determine the truthfullness. So || returns the first true value (or
the last value if they're all false), and && returns the first false
value (or the last value if they're all true).

This allows us to, among other things, use the operators to set
defaults:

my $fruit = $choice || "apples";
#if $chioce is a true value, $fruit = $choice.
#Else, $fruit = "apples";

Paul Lalli

Re: Symbolic representation of logical operators

am 21.08.2007 16:08:15 von markhobley

Paul Lalli wrote:

> Why are you calling this "weirdness"? As has already been explained,
> the logical operators return the last value they have to evaluate to
> determine the truthfullness. So || returns the first true value (or
> the last value if they're all false), and && returns the first false
> value (or the last value if they're all true).

I'm sorry Paul. I am new to Perl, and my understanding is somewhat limited,
and I feel that the documentation is letting me down here. Anyhow, I am trying
to follow this.

You are saying || returns the first true value.

print (2 || 7); # 7, both true, but the first value is 2, why return 7?
print ("apples" || "pears"); # apples, both true, but why return "apples"?

I am still not yet clear on why the numerical one, returns the opposite
argument. My background is assembly language, and electronics, so I think that
is clouding my judgement or thought process. Maybe I need some good documents
that explain this, with lots of examples. I think the issue is that I am not
able to foresee the behaviour from the either the perlop manual (or certainly
not from the page I was looking at, or from my expensive Professional Perl
Programming book.

Looking at &&, you are saying first false, or last value. That is what I
am getting:

print (2 && 7); # 7, Ok, that matches my understanding of your theory
print ("apples" && "pears"); # pears, Ok, so does that

Excellent, thanks for that! (Unless I understand for a wrong reason, maybe, or
maybe don't understand but, think I do, Huh!)

I am still having a problem with XOR:

print ("apples" xor "pears"); s (true), incorrect, should be 0(false)

With "apples" and "pears" both being true, I was expecting a result of false
here.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Re: Symbolic representation of logical operators

am 21.08.2007 17:04:28 von Paul Lalli

On Aug 21, 10:08 am, markhob...@hotpop.deletethisbit.com (Mark Hobley)
wrote:
> Paul Lalli wrote:
> > Why are you calling this "weirdness"? As has already been
> > explained, the logical operators return the last value they
> > have to evaluate to determine the truthfullness. So ||
> > returns the first true value (or the last value if they're
> > all false), and && returns the first false value (or the last
> > value if they're all true).
>
> I'm sorry Paul. I am new to Perl, and my understanding is
> somewhat limited, and I feel that the documentation is letting
> me down here.

No apologies are necessary, I'm sorry if I implied such. I'm just
confused because I thought this was already explained previously in
this thread. Perhaps not.

> Anyhow, I am trying to follow this.
>
> You are saying || returns the first true value.

Yes. Because in order for a chain of arguments connected by || to be
true, only one of those values has to be true. Thus, as soon as we
come to the first true one, Perl can stop looking at the rest. This
is known as short circuit evaluation.

> print (2 || 7); # 7, both true, but the first value is 2,

No it doesn't, unless you have some very old or obscure version of
Perl installed:

$ perl -le'print (2 || 7);'
2
$

When you run that, it produces 7? Really? If so, please copy and
paste the output of this:
perl -v

> print ("apples" || "pears");
> # apples, both true, but why return "apples"?

Because as soon as "apples" was found to be true (all strings other
than "" and "0" are true), there was no need to look at the other
arguments. Perl never even saw "pears" in this evaluation. As soon
as it found one argument to be true, the whole expression is true.

> I am still not yet clear on why the numerical one, returns the
> opposite argument.

It doesn't. Please check that again.

> I think the issue is that I am not able to foresee the behaviour
> from the either the perlop manual (or certainly not from the page
> I was looking at, or from my expensive Professional Perl
> Programming book.

There's really only a couple things you have to make sure you
understand, for this to be clear: The operators (and, or, &&, ||) all
use short circuit evaluation. As soon as it's possible to determine
the results of the entire expression, the operator returns. And they
always return the last value evaluated. For || or or to be true, only
one of the chain of values need be true. For || or or to be false,
they all must be false. The opposite is true for && or and: to be
true, they must all be true. To be false, they must all be false.

If all values are true, || returns the first one, && returns the last:
$x = 1 || 2 || 3 || 4; #1
$x = 1 && 2 && 3 && 4; #4

If all values are false, || returns the last one, && returns the
first:

$x = 0 || "0" || undef || ''; #''
$y = 0 && "0" && undef && ''; #0

If there is a mixture, || returns the first true value, && returns the
first false value:

$x = 0 || 1 || undef || 5; #1
$x = 0 && 1 && undef && 5; #0

> Looking at &&, you are saying first false, or last value.
> That is what I am getting:
>
> print (2 && 7); # 7, Ok, that matches my understanding
> of your theory

It's not a theory, it's simply what is. :-)

> print ("apples" && "pears"); # pears, Ok, so does that
>
> Excellent, thanks for that! (Unless I understand for a wrong
> reason, maybe, or maybe don't understand but, think I do, Huh!)

Nope, you've got it.

> I am still having a problem with XOR:
>
> print ("apples" xor "pears"); s (true), incorrect, should be 0
> (false)

Again, it doesn't return true for me. I don't know what's making you
think it does. For me, it returns the empty string, which is false:

$ perl -le'print ("apples" xor "pears");'

$

> With "apples" and "pears" both being true, I was expecting a
> result of false here.

Yes, and that's exactly what it produces. What output are you seeing
that contradicts that? Please copy and paste your actual code and
output, rather than typing in a comment what you think you're seeing.

Paul Lalli

Re: Symbolic representation of logical operators

am 21.08.2007 17:24:19 von Paul Lalli

On Aug 21, 11:04 am, Paul Lalli wrote:

> There's really only a couple things you have to make sure you
> understand, for this to be clear: The operators (and, or, &&,
> ||) all use short circuit evaluation. As soon as it's possible
> to determine the results of the entire expression, the operator
> returns. And they always return the last value evaluated. For
> || or or to be true, only one of the chain of values need be
> true. For || or or to be false, they all must be false. The
> opposite is true for && or and: to be true, they must all be
> true. To be false, they must all be false.

ACK! That should read:
The opposite is true for && or and: to be true, they must all be
true. To be false, only one need be false.

Sorry about that!
Paul Lalli

Re: Symbolic representation of logical operators

am 21.08.2007 20:52:56 von Michele Dondi

On Tue, 21 Aug 2007 08:04:28 -0700, Paul Lalli
wrote:

>Again, it doesn't return true for me. I don't know what's making you
>think it does. For me, it returns the empty string, which is false:
>
>$ perl -le'print ("apples" xor "pears");'
>
>$
>
>> With "apples" and "pears" both being true, I was expecting a
>> result of false here.
>
>Yes, and that's exactly what it produces. What output are you seeing
>that contradicts that? Please copy and paste your actual code and
>output, rather than typing in a comment what you think you're seeing.

(To the OP) to be sure, try print()ing

"apples" xor "pears" ? 'TRUE' : 'FALSE'


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Symbolic representation of logical operators

am 22.08.2007 17:17:11 von jgraber

Paul Lalli writes:

> On Aug 21, 10:08 am, markhob...@hotpop.deletethisbit.com (Mark Hobley)
> wrote:
> > Paul Lalli wrote:
>
> > print (2 || 7); # 7, both true, but the first value is 2,
>
> No it doesn't, unless you have some very old or obscure version of
> Perl installed:
>
> $ perl -le'print (2 || 7);'
> 2
> $
>
> When you run that, it produces 7? Really? If so, please copy and
> paste the output of this:
> perl -v

This is likely due to a typo on OP part, of | vs ||.
perl -le 'print (2||7)' # prints 2, logical or
perl -le 'print (2|7)' # prints 7, bitwise num or: 0010b | 0111b = 0111b
perl -le 'print (2|8)' # prints 10, bitwise num or: 0010b | 1000b = 1010b
also note:
perl -le 'print ("2"|"7")' # prints 7, bitwise ascii or:
# 00110010b = '2'
# | 00110111b = '7' = 00110111b = '7'
perl -le 'print ("2"|"8")' # prints :, bitwise ascii or:
# 00110010b = '2'
# | 00111000b = '7' = 00111010b = ':'
--
Joel

Re: Symbolic representation of logical operators

am 23.08.2007 15:43:37 von Ben Morrow

Quoth Michele Dondi :
> On Tue, 21 Aug 2007 08:04:28 -0700, Paul Lalli
> wrote:
>
> >Again, it doesn't return true for me. I don't know what's making you
> >think it does. For me, it returns the empty string, which is false:
> >
> >$ perl -le'print ("apples" xor "pears");'
> >
> >$
> >
> >> With "apples" and "pears" both being true, I was expecting a
> >> result of false here.
> >
> >Yes, and that's exactly what it produces. What output are you seeing
> >that contradicts that? Please copy and paste your actual code and
> >output, rather than typing in a comment what you think you're seeing.
>
> (To the OP) to be sure, try print()ing
>
> "apples" xor "pears" ? 'TRUE' : 'FALSE'

ITYM

("apples" xor "pears") ? 'TRUE' : 'FALSE'

And the OP needs to realise that to print this one must use

print( ("apples" xor "pears") ? 'TRUE' : 'FALSE' );

due to Perl's 'if it looks like a function it parses as a function'
rule.

Ben

--
don't get my sympathy hanging out the 15th floor. you've changed the locks 3
times, he still comes reeling though the door, and soon he'll get to you, teach
you how to get to purest hell. you do it to yourself and that's what really
hurts is you do it to yourself just you, you and noone else ** ben@morrow.me.uk
--
Every twenty-four hours about 34k children die from the effects of poverty.
Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
that image, that ghastly, grey-billowing, double-barrelled fall, repeated
twelve times every day. Full of children. [Iain Banks] ben@morrow.me.uk

Re: Symbolic representation of logical operators

am 23.08.2007 18:36:23 von Michele Dondi

On Thu, 23 Aug 2007 14:43:37 +0100, Ben Morrow
wrote:

>> "apples" xor "pears" ? 'TRUE' : 'FALSE'
>
>ITYM
>
> ("apples" xor "pears") ? 'TRUE' : 'FALSE'

Yep, sorry!

>And the OP needs to realise that to print this one must use
>
> print( ("apples" xor "pears") ? 'TRUE' : 'FALSE' );
>
>due to Perl's 'if it looks like a function it parses as a function'
>rule.

Or

print +("apples" xor "pears") ? 'TRUE' : 'FALSE';

which I like and confuses me less, but maybe that's just me. Come to
think of it, I must have never used C.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Symbolic representation of logical operators

am 24.08.2007 03:31:42 von Tad McClellan

Michele Dondi wrote:
> On Thu, 23 Aug 2007 14:43:37 +0100, Ben Morrow
> wrote:
>
>>> "apples" xor "pears" ? 'TRUE' : 'FALSE'
>>
>>ITYM
>>
>> ("apples" xor "pears") ? 'TRUE' : 'FALSE'
>
> Yep, sorry!
>
>>And the OP needs to realise that to print this one must use
>>
>> print( ("apples" xor "pears") ? 'TRUE' : 'FALSE' );
>>
>>due to Perl's 'if it looks like a function it parses as a function'
>>rule.
>
> Or
>
> print +("apples" xor "pears") ? 'TRUE' : 'FALSE';
>
> which I like and confuses me less, but maybe that's just me. Come to
> think of it, I must have never used C.


The rule is a consequence of having optional parenthesis around
a function's argument list.

See also:

Message-Id:


The least confusing "fix" is to use parenthesis around the
function's argument list, even if you don't normally use them:

print(("apples" xor "pears") ? 'TRUE' : 'FALSE');



--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Symbolic representation of logical operators

am 24.08.2007 09:14:00 von Michele Dondi

On Fri, 24 Aug 2007 01:31:42 GMT, Tad McClellan
wrote:

>The least confusing "fix" is to use parenthesis around the
>function's argument list, even if you don't normally use them:
>
> print(("apples" xor "pears") ? 'TRUE' : 'FALSE');

Confusion is in the eye of the beholder.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Symbolic representation of logical operators

am 24.08.2007 10:33:10 von hjp-usenet2

On 2007-08-19 20:08, Mark Hobley wrote:
> print (7 xor 2); # Empty string, I was expecting 0 (true xor true = false)

The empty string is also false, and all operators which return a "pure"
truth value (==, !=, <, >, <=, >=, !) return 1 as true and an empty
string as false. So xor is consistent with the rest of perl (although
not consistent with any other programming language I know).

The empty string returned by these operators is actually quite an
interesting piece: It is both an empty string and the number 0 at the
same time:

#!/usr/bin/perl
use warnings;
use strict;

my $x = "";
print "\$x=<$x>\n"; # Prints "<>"
my $y = $x + 5; # Warning: Argument "" isn't numeric in addition (+)

$x = !"b";
print "\$x=<$x>\n"; # Prints "<>"
$y = $x + 5; # No warning

hp


--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hjp@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"