On 1/7/2025 10:03 PM, Jeong Dal via ntg-context wrote:
Dear Hans,
It is nice to input the function as an argument.
It is suffice to convey one function at each all.
But the definition of the function
local function logicF3(p,q,r)
return ((not p or not q) and r) or (p and (q or not r))
-- return ((p == "0" or q == "0") and r == "1") or (p == "1" and (q
== "1" or r == "0"))
end
Is not working. The outputs are blank(for false) and “?”(for true)
Here is a working example.
Thank you for enhancing the code.
Best regards,
Dalyoung
\startluacode
function document.MakeHead(p,a)
if not a then
local t = p
p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|"
a = t
end
context.starttabulate { p }
context.FL()
for i=1,#a do
context.NC() context(a[i])
end
context.NC() context.NR()
context.LL()
end
function document.MakeFooter()
context.HL()
context.stoptabulate()
end
local tf = { true, false }
function document.truthTable(a,func)
document.MakeHead(a)
for i,s in ipairs(tf) do
for j,t in ipairs(tf) do
for k,u in ipairs(tf) do
context.NC()
context(s and "1" or "0")
context.NC()
context(t and "1" or "0")
context.NC()
if #a == 3 then
break
end
context(u and "1" or "0")
context.NC()
context(func(s,t,u))
context.NC()
context.AR()
end
if #a == 3 then
context(func(s,t))
context.NC()
context.AR()
end
end
end
document.MakeFooter()
end
\stopluacode
—- We define a logic function before we call the main function.
—- by changing the function, we get the result for two variables.
\startluacode
--statistics.starttiming()
local function logicF(p,q,r)
if (not q) and (p or r) then
return "1"
else
return "0"
end
—- return (not q) and (p or r)
end
document.truthTable({ "x", "y","z","f(x,y,x)"},logicF)
\stopluacode
\stoptext
the magic has to come from:
context(func(s,t,u) and "1" or "0")
context(func(s,t) and "1" or "0")
then
local function logicF(p,q,r)
return (not q) and (p or r)
end
should work
\startluacode
function document.MakeHead(p,a)
if not a then
local t = p
p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|"
a = t
end
context.starttabulate { p }
context.FL()
for i=1,#a do
context.NC() context(a[i])
end
context.NC() context.NR()
context.LL()
end
function document.MakeFooter()
context.HL()
context.stoptabulate()
end
local tf = { true, false }
function document.truthTable(a,func)
document.MakeHead(a)
for i,s in ipairs(tf) do
for j,t in ipairs(tf) do
for k,u in ipairs(tf) do
context.NC()
context(s and "1" or "0")
context.NC()
context(t and "1" or "0")
context.NC()
if #a == 3 then
break
end
context(u and "1" or "0")
context.NC()
context(func(s,t,u) and "1" or "0")
context.NC()
context.AR()
end
if #a == 3 then
context(func(s,t) and "1" or "0")
context.NC()
context.AR()
end
end
end
document.MakeFooter()
end
\stopluacode
\startluacode
local function logicF(p,q,r)
return (not q) and (p or r)
end
document.truthTable({ "x", "y","z","f(x,y,x)"},logicF)
\stopluacode
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the
Wiki!
maillist : ntg-context@ntg.nl /
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________