<div dir="ltr"><div><div>Nice one!<br>It's the binding of the xor, the correct code is return (($self->true_count($idx) > 0) xor ($self->false_count($idx) > 0));<br></div>return a xor b is equivalent to return(a) xor b.<br>
<br></div>Wim<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 28 April 2013 18:57, Miles Gould <span dir="ltr"><<a href="mailto:miles@assyrian.org.uk" target="_blank">miles@assyrian.org.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
I recently encountered some Perl behaviour that surprised me, and thought you might be amused and/or able to provide further insight.<br>
<br>
Here's the buggy function:<br>
<br>
sub is_unate {<br>
my ($self, $idx) = @_;<br>
return ($self->true_count($idx) > 0) xor ($self->false_count($idx) > 0);<br>
}<br>
<br>
and here are the tests which allowed me to diagnose the problem:<br>
<br>
is($cubes->true_count(1), 1);<br>
is($cubes->true_count(2), 1);<br>
is($cubes->true_count(3), 1);<br>
is($cubes->true_count(4), 1);<br>
is($cubes->true_count(5), 1);<br>
<br>
is($cubes->false_count(1), 1);<br>
is($cubes->false_count(2), 0);<br>
is($cubes->false_count(3), 1);<br>
is($cubes->false_count(4), 1);<br>
is($cubes->false_count(5), 0);<br>
<br>
ok(!$cubes->is_unate(1)); # fails<br>
ok($cubes->is_unate(2));<br>
ok(!$cubes->is_unate(3)); # fails<br>
ok(!$cubes->is_unate(4)); # fails<br>
ok($cubes->is_unate(5));<br>
<br>
All tests pass apart from the ones marked "fails".<br>
<br>
So, my challenges:<br>
<br>
1) Can you spot the bug in my code, and find a fix for it?<br>
2) Can you explain why it makes sense for Perl to behave this way?<br>
<br>
Miles<br>
______________________________<u></u>_________________<br>
Edinburgh-pm mailing list<br>
<a href="mailto:Edinburgh-pm@pm.org" target="_blank">Edinburgh-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/edinburgh-pm" target="_blank">http://mail.pm.org/mailman/<u></u>listinfo/edinburgh-pm</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>If it's pointless, what's the point?<br>If there is a point to it, what's the point?<br>(Tibor Fischer, "The Thought Gang")
</div>