Hi all,
Thank you for your contributions, it allows me to progress.
@Hans and Deal
The code is impressive but does not correspond to the shape of the triangle


that I have to make (by convention).
Fabrice

Le lun. 20 juil. 2020 à 10:36, Hans Hagen <j.hagen@xs4all.nl> a écrit :
On 7/20/2020 7:56 AM, Jeong Dal wrote:
> Dear Fabrice,
>
> You may split Binom(n,k) function into two functions as following:
>
 > see original mail
 >
> Dalyoung
Best stay in a protected namespace ...

\startluacode

     local function fact (n)
         if n <= 0 then
             return 1
         else
             return n * fact(n-1)
         end
     end

     local function ncr(n,r)
         return fact(n)/(fact(r)*fact(n-r))
     end

     userdata.P = {
         fact = fact,
         ncr  = ncr,
     }

     function MP.pascal_ncr(n, r)
         mp.print(ncr(n,r))
     end

\stopluacode

Watch the last definition. This permits

       % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," &
decimal r & " ))");

replaced by

         tt := lua.MP.pascal_ncr(n,r);

which looks nicer.

\startbuffer[pt1]
     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;
           % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," &
decimal r & " ))");
           tt := lua.MP.pascal_ncr(n,r);
             now := start+r*right*dx;
             dotlabel.top(textext("$\displaystyle {" & decimal n &
"\choose" & decimal r & "} = "& decimal tt & "$"),now);
             draw (now+A) -- now -- (now+B);
         endfor;
     endfor;
\stopbuffer

Now, in context lmtx we can have a different kind of abstraction. We can
do this:

     function MP.pascal_ncr_x()
         mp.print(ncr(mp.scan.pair()))
     end

and then use:

     tt := runscript("MP.pascal_ncr_x()") (n,r) ;

Of course one can decide to pick to two numerics instead, like

     tt := runscript("MP.pascal_ncr_x()") n r ;

but i leave that as exercise.

           % tt := runscript mp_pascal_ncr (n,r) ;
             tt := pascal_ncr (n,r) ;

However, we still have the rather verbose runscript here, so we go
further, we register pascal as script:

\startluacode
     metapost.registerscript("pascal_ncr",MP.pascal_ncr_x)
\stopluacode

And then define an alias at the metafun end:

\startMPextensions
     newinternal mp_pascal_ncr ; mp_pascal_ncr := scriptindex "pascal_ncr" ;

     def pascal_ncr =
         runscript mp_pascal_ncr
     enddef ;
\stopMPextensions

The internal permits this:

    tt := runscript mp_pascal_ncr (n,r) ;

while the additional def permits

             tt := pascal_ncr (n,r) ;

Now watch out, because we define pascal_ncr here, something
lua.MP.pascal_ncr(n,r) won't work because the last part gets expanded
because that is what mp does (i'll probably cook something for that some
day).

Now, to come back to

    "I couldn’t wikify it at that time because I don’t know
     how to.  I’ll do it soon."

looks like you suddenly have an additional challenge,

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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________