Ternary logic: Difference between revisions
Content deleted Content added
No edit summary |
SqrtNegInf (talk | contribs) m →{{header|Perl}}: a single file of runnable code |
||
Line 4,269:
=={{header|Perl}}==
<syntaxhighlight lang="perl">
use warnings;
use feature 'say';
package Trit;
▲# -1 = false ; 0 = maybe ; 1 = true
our @ISA = qw(Exporter);
▲ const => [qw(TRUE FALSE MAYBE)],
use List::Util qw(min max);
Line 4,303 ⟶ 4,299:
;
sub new {
my ($class, $v) = @_;
!defined($v) ? 0 :
▲ return bless \$ret, $class;
}
Line 4,321 ⟶ 4,316:
sub MAYBE() { new Trit( 0) }
sub clone {
my $ret = ${$_[0]};
}
sub tostr { ${$_[0]} > 0 ?
sub tonum { ${$_[0]} }
sub cmp { ${$_[0]} <=> ${$_[1]} }
Line 4,339 ⟶ 4,329:
sub or { new Trit(max(${$_[0]}, ${$_[1]}) ) }
sub equiv { new Trit( ${$_[0]} * ${$_[1]} ) }
package main;
Trit->import;
my @a = (TRUE(), MAYBE(), FALSE());
# prefix ! (not) ['~' also can be used]
# infix & (and)
for my $a (@a) { print $a; print "\t" . ($a & $_) for @a; say '' }
# infix | (or)
▲print "\nAND\t".join("\t",@a)."\n";
for my $a (@a) { print $a; print "\t" . ($a | $_) for @a; say '' }
# infix eq (equivalence)
▲print "\nOR\t".join("\t",@a)."\n";
for my $a (@a) { print $a; print "\t" . ($a eq $_) for @a; say '' }
▲print "\nEQV\t".join("\t",@a)."\n";
# infix == (equality)
▲print "\n==\t".join("\t",@a)."\n";
say "\n==\t".join("\t",@a);
for my $a (@a) { print $a; print "\t" . ($a == $_) for @a; say '' }</syntaxhighlight>
{{out}}
<pre>a NOT a
|