Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
57
Task/Ternary-logic/Nim/ternary-logic.nim
Normal file
57
Task/Ternary-logic/Nim/ternary-logic.nim
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
type Trit* = enum ttrue, tmaybe, tfalse
|
||||
|
||||
proc `$`*(a: Trit): string =
|
||||
case a
|
||||
of ttrue: "T"
|
||||
of tmaybe: "?"
|
||||
of tfalse: "F"
|
||||
|
||||
proc `not`*(a: Trit): Trit =
|
||||
case a
|
||||
of ttrue: tfalse
|
||||
of tmaybe: tmaybe
|
||||
of tfalse: ttrue
|
||||
|
||||
proc `and`*(a, b: Trit): Trit =
|
||||
const t: array[Trit, array[Trit, Trit]] =
|
||||
[ [ttrue, tmaybe, tfalse]
|
||||
, [tmaybe, tmaybe, tfalse]
|
||||
, [tfalse, tfalse, tfalse] ]
|
||||
t[a][b]
|
||||
|
||||
proc `or`*(a, b: Trit): Trit =
|
||||
const t: array[Trit, array[Trit, Trit]] =
|
||||
[ [ttrue, ttrue, ttrue]
|
||||
, [ttrue, tmaybe, tmaybe]
|
||||
, [ttrue, tmaybe, tfalse] ]
|
||||
t[a][b]
|
||||
|
||||
proc then*(a, b: Trit): Trit =
|
||||
const t: array[Trit, array[Trit, Trit]] =
|
||||
[ [ttrue, tmaybe, tfalse]
|
||||
, [ttrue, tmaybe, tmaybe]
|
||||
, [ttrue, ttrue, ttrue] ]
|
||||
t[a][b]
|
||||
|
||||
proc equiv*(a, b: Trit): Trit =
|
||||
const t: array[Trit, array[Trit, Trit]] =
|
||||
[ [ttrue, tmaybe, tfalse]
|
||||
, [tmaybe, tmaybe, tmaybe]
|
||||
, [tfalse, tmaybe, ttrue] ]
|
||||
t[a][b]
|
||||
|
||||
import strutils
|
||||
|
||||
var
|
||||
op1 = ttrue
|
||||
op2 = ttrue
|
||||
|
||||
for t in Trit:
|
||||
echo "Not ", t , ": ", not t
|
||||
|
||||
for op1 in Trit:
|
||||
for op2 in Trit:
|
||||
echo "$# and $#: $#".format(op1, op2, op1 and op2)
|
||||
echo "$# or $#: $#".format(op1, op2, op1 or op2)
|
||||
echo "$# then $#: $#".format(op1, op2, op1.then op2)
|
||||
echo "$# equiv $#: $#".format(op1, op2, op1.equiv op2)
|
||||
53
Task/Ternary-logic/Phix/ternary-logic.phix
Normal file
53
Task/Ternary-logic/Phix/ternary-logic.phix
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
enum type ternary T, M, F end type
|
||||
|
||||
function t_not(ternary a)
|
||||
return F+1-a
|
||||
end function
|
||||
|
||||
function t_and(ternary a, ternary b)
|
||||
return iff(a=T and b=T?T:iff(a=F or b=F?F:M))
|
||||
end function
|
||||
|
||||
function t_or(ternary a, ternary b)
|
||||
return iff(a=T or b=T?T:iff(a=F and b=F?F:M))
|
||||
end function
|
||||
|
||||
function t_xor(ternary a, ternary b)
|
||||
return iff(a=M or b=M?M:iff(a=b?F:T))
|
||||
end function
|
||||
|
||||
function t_implies(ternary a, ternary b)
|
||||
return iff(a=F or b=T?T:iff(a=T and b=F?F:M))
|
||||
end function
|
||||
|
||||
function t_equal(ternary a, ternary b)
|
||||
return iff(a=M or b=M?M:iff(a=b?T:F))
|
||||
end function
|
||||
|
||||
function t_string(ternary a)
|
||||
return iff(a=T?"T":iff(a=M?"?":"F"))
|
||||
end function
|
||||
|
||||
procedure show_truth_table(integer rid, integer unary, string name)
|
||||
printf(1,"%-3s |%s\n",{name,iff(unary?"":" T | ? | F")})
|
||||
printf(1,"----+---%s\n",{iff(unary?"":"+---+---")})
|
||||
for x=T to F do
|
||||
printf(1," %s ",{t_string(x)})
|
||||
if unary then
|
||||
printf(1," | %s",{t_string(call_func(rid,{x}))})
|
||||
else
|
||||
for y=T to F do
|
||||
printf(1," | %s",{t_string(call_func(rid,{x,y}))})
|
||||
end for
|
||||
end if
|
||||
printf(1,"\n")
|
||||
end for
|
||||
printf(1,"\n")
|
||||
end procedure
|
||||
|
||||
show_truth_table(routine_id("t_not"),1,"not")
|
||||
show_truth_table(routine_id("t_and"),0,"and")
|
||||
show_truth_table(routine_id("t_or"),0,"or")
|
||||
show_truth_table(routine_id("t_xor"),0,"xor")
|
||||
show_truth_table(routine_id("t_implies"),0,"imp")
|
||||
show_truth_table(routine_id("t_equal"),0,"eq")
|
||||
32
Task/Ternary-logic/jq/ternary-logic.jq
Normal file
32
Task/Ternary-logic/jq/ternary-logic.jq
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
def ternary_nand(a; b):
|
||||
if a == false or b == false then true
|
||||
elif a == "maybe" or b == "maybe" then "maybe"
|
||||
else false
|
||||
end ;
|
||||
|
||||
def ternary_not(a): ternary_nand(a; a);
|
||||
|
||||
def ternary_or(a; b): ternary_nand( ternary_not(a); ternary_not(b) );
|
||||
|
||||
def ternary_nor(a; b): ternary_not( ternary_or(a;b) );
|
||||
|
||||
def ternary_and(a; b): ternary_not( ternary_nand(a; b) );
|
||||
|
||||
def ternary_imply(this; that):
|
||||
ternary_nand(this, ternary_not(that));
|
||||
|
||||
def ternary_equiv(this; that):
|
||||
ternary_or( ternary_and(this; that); ternary_nor(this; that) );
|
||||
|
||||
def display_and(a; b):
|
||||
a as $a | b as $b
|
||||
| "\($a) and \($b) is \( ternary_and($a; $b) )";
|
||||
def display_equiv(a; b):
|
||||
a as $a | b as $b
|
||||
| "\($a) equiv \($b) is \( ternary_equiv($a; $b) )";
|
||||
# etc etc
|
||||
|
||||
# Invoke the display functions:
|
||||
display_and( (false, "maybe", true ); (false, "maybe", true) ),
|
||||
display_equiv( (false, "maybe", true ); (false, "maybe", true) ),
|
||||
"etc etc"
|
||||
Loading…
Add table
Add a link
Reference in a new issue