Loïc Grenié on Fri, 13 Feb 2015 13:33:00 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Function names


2015-02-13 12:23 GMT+01:00 Josef Eschgfaeller <esg@unife.it>:
> Is there a way to obtain the name
> of a function as a string? I would like
> to generate an output of the form
> --------------------------------------------------
> f   3
> g  9
> h 27
> --------------------------------------------------
> after
> --------------------------------------------------
> f (x) = x
> g (x) = x^2
> h (x) = x^3
>
> F=[f,g,h]
> for (i=1,3, f=F[i]; print(Strprintf("%s: %d",f,f(3))))
> --------------------------------------------------
> But this (as happens with Str(f)) prints the
> function bodies.

    This is because variable f contains the body of the function.
  You can do the contrary, though:

F=["f","g","h"]
for(i=1,3,my(f=eval(F[i]));print(F[i], ": ", f(3)))

     Hope this helps,

          Loïc