On 1/7/2025 3:27 PM, Jeong Dal via ntg-context wrote:
Dear Hans,
I tested your code.
local tf = { true, false }
function document.truthTable(a) document.MakeHead(a) … …
end document.MakeFooter() end
This part produces the right logical value. But, the next part doesn’t produce the correct value. While the value of each variable is printed correctly, the values of outputs are all 0.
local tf = { "1", "0" }
function document.truthTable(a) 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) context.NC() context(t) context.NC() … ... end end end document.MakeFooter() end
I think that the logical function “document.logicF()"is not working for the array tf={“1”,”0”}. In python, 1 and 0 are treated as true and false. But, I read that only “false” and “nil” are treated as false in Lua, that is, “0” is also treated as true! I checked it using the following simple code:
\starttext \startluacode if 0 then context("True") else context("False") end \stopluacode \stoptext
The output The output is “True”.
Ah, i didn't check the function, so how about this: local tf = { true, false } --local tf = { "1", "0" } function document.truthTable(a,f3,f2) 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(s) context.NC() context(t and "1" or "0") -- context(t) context.NC() if #a == 3 then break end context(u and "1" or "0") -- context(u) context.NC() context(f3(s,t,u) and "1" or "0") context.NC() context.AR() end if #a == 3 then context(f2(s,t) and "1" or "0") context.NC() context.AR() end end end document.MakeFooter() end and then you can do \startluacode 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 local function logicF2(p,q) -- todo return true end document.truthTable ( { "p", "q", "r", "f(p,q,r)" }, logicF3, logicF2 ) \stopluacode so no need to store in document.logicXX and no need for that function to know about "0" and "1" being rendered, (btw, logicF2 was not defined in the example) Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------