Ternary logic: Difference between revisions

Added Forth entry
(Added solution for Action!)
(Added Forth entry)
Line 1,895:
( scratchpad ) trits [ trits swap [ t= ] curry map ] map .
{ { t m f } { m m m } { f m t } }</lang>
 
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
 
Standard Forth defines flags 'false' as 0 and 'true' as -1 (all bits set). We thus define 'maybe' as 1 to keep standard binary logic as-is and seamlessly include ternary logic. We may have use truthtables but here functions are more fluid.
 
<lang forth>1 constant maybe
 
: tnot dup maybe <> if invert then ;
: tand and ;
: tor or ;
: tequiv 2dup and rot tnot rot tnot and or ;
: timply tnot tor ;
: txor tequiv tnot ;
 
: t. C" TF?" 2 + + c@ emit ;
 
: table2. ( xt -- )
cr ." T F ?"
cr ." --------"
2 true DO
cr I t. ." | "
2 true DO
dup I J rot execute t. ." "
LOOP
LOOP DROP ;
 
: table1. ( xt -- )
2 true DO
CR I t. ." | "
dup I swap execute t.
LOOP DROP ;
 
CR ." [NOT]" ' tnot table1. CR
CR ." [AND]" ' tand table2. CR
CR ." [OR]" ' tor table2. CR
CR ." [XOR]" ' txor table2. CR
CR ." [IMPLY]" ' timply table2. CR
CR ." [EQUIV]" ' tequiv table2. CR
</lang>
 
{{out}}
<pre>[NOT]
T | F
F | T
? | ?
 
[AND]
T F ?
--------
T | T F ?
F | F F F
? | ? F ?
 
[OR]
T F ?
--------
T | T T T
F | T F ?
? | T ? ?
 
[XOR]
T F ?
--------
T | F T ?
F | T F ?
? | ? ? ?
 
[IMPLY]
T F ?
--------
T | T F ?
F | T T T
? | T ? ?
 
[EQUIV]
T F ?
--------
T | T F ?
F | F T ?
? | ? ? ?
</pre>
 
As Forth is a concatenative language, ternary logic use appears seamless:
<pre>: optimist CR or if ." yes !" else ." no..." then ; ok
true maybe optimist
yes ! ok
maybe false optimist
yes ! ok
maybe maybe optimist
yes ! ok
false false optimist
no... ok
</pre>
 
 
=={{header|Fortran}}==
Anonymous user