Ternary logic: Difference between revisions
Content added Content deleted
(→{{header|Pascal}}: fix wrong logic and indentation) |
(→{{header|Groovy}}: new solution) |
||
Line 1,138: | Line 1,138: | ||
True True True |
True True True |
||
</pre> |
</pre> |
||
=={{header|Groovy}}== |
|||
Solution: |
|||
<lang groovy>enum Trit { |
|||
TRUE, MAYBE, FALSE |
|||
private Trit nand(Trit that) { |
|||
switch ([this,that]) { |
|||
case { FALSE in it }: return TRUE |
|||
case { MAYBE in it }: return MAYBE |
|||
default : return FALSE |
|||
} |
|||
} |
|||
private Trit nor(Trit that) { this.or(that).not() } |
|||
Trit and(Trit that) { this.nand(that).not() } |
|||
Trit or(Trit that) { this.not().nand(that.not()) } |
|||
Trit not() { this.nand(this) } |
|||
Trit imply(Trit that) { this.nand(that.not()) } |
|||
Trit equiv(Trit that) { this.and(that).or(this.nor(that)) } |
|||
}</lang> |
|||
Test: |
|||
<lang groovy>printf 'AND\n ' |
|||
Trit.values().each { b -> printf ('%6s', b) } |
|||
println '\n ----- ----- -----' |
|||
Trit.values().each { a -> |
|||
printf ('%6s | ', a) |
|||
Trit.values().each { b -> printf ('%6s', a.and(b)) } |
|||
println() |
|||
} |
|||
printf '\nOR\n ' |
|||
Trit.values().each { b -> printf ('%6s', b) } |
|||
println '\n ----- ----- -----' |
|||
Trit.values().each { a -> |
|||
printf ('%6s | ', a) |
|||
Trit.values().each { b -> printf ('%6s', a.or(b)) } |
|||
println() |
|||
} |
|||
println '\nNOT' |
|||
Trit.values().each { |
|||
printf ('%6s | %6s\n', it, it.not()) |
|||
} |
|||
printf '\nIMPLY\n ' |
|||
Trit.values().each { b -> printf ('%6s', b) } |
|||
println '\n ----- ----- -----' |
|||
Trit.values().each { a -> |
|||
printf ('%6s | ', a) |
|||
Trit.values().each { b -> printf ('%6s', a.imply(b)) } |
|||
println() |
|||
} |
|||
printf '\nEQUIV\n ' |
|||
Trit.values().each { b -> printf ('%6s', b) } |
|||
println '\n ----- ----- -----' |
|||
Trit.values().each { a -> |
|||
printf ('%6s | ', a) |
|||
Trit.values().each { b -> printf ('%6s', a.equiv(b)) } |
|||
println() |
|||
}</lang> |
|||
Output: |
|||
<pre>AND |
|||
TRUE MAYBE FALSE |
|||
----- ----- ----- |
|||
TRUE | TRUE MAYBE FALSE |
|||
MAYBE | MAYBE MAYBE FALSE |
|||
FALSE | FALSE FALSE FALSE |
|||
OR |
|||
TRUE MAYBE FALSE |
|||
----- ----- ----- |
|||
TRUE | TRUE TRUE TRUE |
|||
MAYBE | TRUE MAYBE MAYBE |
|||
FALSE | TRUE MAYBE FALSE |
|||
NOT |
|||
TRUE | FALSE |
|||
MAYBE | MAYBE |
|||
FALSE | TRUE |
|||
IMPLY |
|||
TRUE MAYBE FALSE |
|||
----- ----- ----- |
|||
TRUE | TRUE MAYBE FALSE |
|||
MAYBE | TRUE MAYBE MAYBE |
|||
FALSE | TRUE TRUE TRUE |
|||
EQUIV |
|||
TRUE MAYBE FALSE |
|||
----- ----- ----- |
|||
TRUE | TRUE MAYBE FALSE |
|||
MAYBE | MAYBE MAYBE MAYBE |
|||
FALSE | FALSE MAYBE TRUE</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |