On 4/11/2020 4:15 PM, Jeong Dal wrote:
Dear all,
The problem is solved by using the namespace of lua as below:
I am not sure what I did is the right way.
If it is not the right way, please let me know.
Thanks for reading.
Best regards,
Dalyoung
\startluacode
P={}
combi = P
function P.fact (n)
if n <= 0 then
return 1
else
return n * P.fact(n-1)
end
end
function P.ncr(n,r)
return P.fact(n)/(P.fact(r)*P.fact(n-r))
end
combi = {
fact = fact,
ncr = ncr,
}
\stopluacode
\startbuffer[fig121]
numeric n,r,s,u,dx,dy,tt; u := 1.8cm;
path p, q;
pair A,B,start,now;
A := dir(210)*u;
B := dir(-30)*u;
dy := sind(30)*u;
dx := 2*cosd(30)*u;
for n=0 upto 4:
start := n*dir(210)*u;
for r=0 upto n:
s := n-r;
% tt := lua("mp.print(P.fact(" & decimal n & ")/(P.fact(" & decimal r &
")*P.fact(" & decimal s &" )))");
tt := lua("mp.print(P.ncr(" & decimal n & "," & decimal r & " ))");
now := start+r*right*dx;
dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" &
decimal r & "} = "& decimal tt & "$"),now);
draw now -- (now+A);
draw now -- (now+B);
endfor;
endfor;
\stopbuffer
Sunday afternoon educational moment (that you can wikify), four variants:
1 : A more metafunish alternative of your example.
2 : The same but avoiding a temporary variable.
3 : Less code and clutter, the real deal.
4 : Idem, but permits more tuning at the TeX end.
\unexpanded\def\MyWhatever#1#2#3%
{$\displaystyle{#1\choose #2} = #3$}
\startluacode
local function fact(n)
if n <= 0 then
return 1
else
return n * fact(n - 1)
end
end
local function whatever(n,r)
return fact(n) / (fact(r) * fact(n-r))
end
MP.WhateverA = whatever
function MP.WhateverB(n,r)
mp.quoted("%.0f",whatever(n,r))
end
function MP.WhateverC(n,r)
mp.quoted([[$\displaystyle{%.0f\choose %.0f} =
%.0f$]],n,r,whatever(n,r))
end
function MP.WhateverD(n,r)
mp.quoted([[\MyWhatever{%.0f}{%.0f}{%.0f}]],n,r,whatever(n,r))
end
\stopluacode
\startbuffer[fig121]
numeric n, r, s, u, dx, dy, tt;
path p, q ; pair A, B, start, now;
u := 1.8cm;
A := dir(210)*u;
B := dir(-30)*u;
dy := sind(30)*u;
dx := 2*cosd(30)*u;
for n = 0 upto 4:
start := n*dir(210)*u;
for r = 0 upto n:
s := n - r;
now := start + r*right*dx;
draw (now + A) -- now -- (now + B);
tt := lua.MP.WhateverA(n,r) ;
dotlabel.top(textext("$\displaystyle {" & decimal n &
"\choose" & decimal r & "} = "& decimal tt & "$"),now);
dotlabel.top(textext("$\displaystyle {" & decimal n &
"\choose" & decimal r & "} = "& lua.MP.WhateverB(n,r) & "$"),now);
dotlabel.top(textext(lua.MP.WhateverC(n,r)),now);
dotlabel.top(textext(lua.MP.WhateverD(n,r)),now);
endfor;
endfor;
\stopbuffer
\starttext
{\switchtobodyfont[11pt]\processMPbuffer[fig121]}
\stoptext
-----------------------------------------------------------------
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 / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________