Ternary logic: Difference between revisions

No edit summary
Line 2,964:
 
L3.iff(a, 2 == 2) // undefined
</lang>
Here is a compact alternate solution
<lang Javascript>
nand = (a, b) => (a == false || b == false) ? true : (a == 'maybe' || b == 'maybe') ? 'maybe' : false;
not = (a) => (a == 'maybe') ? 'maybe' : !a;
and = (a, b) => not(nand(a, b));
or = (a, b) => nand(not(a), not(b));
imply= (a, b) => nand(a, not(b));
iff = (a, b) => or(and(a, b), not(or(a, b)));
</lang>
... to test it
<lang Javascript>
['nand', 'and', 'or', 'imply', 'iff'].forEach(
op => {
console.log('\n----> ' + op);
[false, 'maybe', true].forEach(
a => {
[false, 'maybe', true].forEach(
b => console.log ('' + a + ' ' + op + ' ' + b + ' = ' + eval(op)(a, b))
)
}
)
}
)
 
console.log('\n----> not');
[false, 'maybe', true].forEach(
a => console.log ('not ' + a + ' = ' + not(a))
)
</lang>
...Output:
<lang>
 
----> nand
false nand false = true
false nand maybe = true
false nand true = true
maybe nand false = true
maybe nand maybe = maybe
maybe nand true = maybe
true nand false = true
true nand maybe = maybe
true nand true = false
 
----> and
false and false = false
false and maybe = false
false and true = false
maybe and false = false
maybe and maybe = maybe
maybe and true = maybe
true and false = false
true and maybe = maybe
true and true = true
 
----> or
false or false = false
false or maybe = maybe
false or true = true
maybe or false = maybe
maybe or maybe = maybe
maybe or true = true
true or false = true
true or maybe = true
true or true = true
 
----> imply
false imply false = true
false imply maybe = true
false imply true = true
maybe imply false = maybe
maybe imply maybe = maybe
maybe imply true = true
true imply false = false
true imply maybe = maybe
true imply true = true
 
----> iff
false iff false = true
false iff maybe = maybe
false iff true = false
maybe iff false = maybe
maybe iff maybe = maybe
maybe iff true = maybe
true iff false = false
true iff maybe = maybe
true iff true = true
 
----> not
not false = true
not maybe = maybe
not true = false
</lang>