MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Function Name from Inside the Function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg71549] Function Name from Inside the Function
  • From: "Philipp" <Philipp.M.O at gmail.com>
  • Date: Wed, 22 Nov 2006 05:22:29 -0500 (EST)

Is there a way to get a function name from inside the function during
the execution time?

One reason is that it would greatly aid debugging.

To illustrate the concept, let's assume that the function names are
stored in the list called $FunctionNames. Now,

In[]:=   Clear[f, g, h];
         f[x_] := (Print[{Last[#], #} &[$FunctionNames]]; x^2)
         g[x_] := (Print[{Last[#], #} &[$FunctionNames]]; f[x] + 5)
         h[x_] := Module[{fname = Last[$FunctionNames]},
                   Print[{Last[#], #} &[$FunctionNames]];
                   If[MemberQ[debugSymbols, fname],
                     Print["Debug in ", fname, ", x = ", x]]; g[x]]

In[]:=   debugSymbols = {"h"};
         h[5]

         {h, {h}}

         Debug in h, x = 5

         {g, {h, g}}

         {f, {h, g, f}}

Out[]=   30

(The Print[{Last[#], #} &[$FunctionNames]] statements are only here to
demonstrate the concept; they wouldn't be normally present.)

So, at every function execution a "trace" of functions is available
(recursion would be a pain to handle, though).

Is there anything similar available in Mathematica? Even only the
function name currently executing instead of the "trace" list.

Cheers,
Philipp.


  • Prev by Date: Bookmarks in Mathematica ????
  • Next by Date: Re: ComplexityFunction affects set of transformations tried by Simplify
  • Previous by thread: RE: Bookmarks in Mathematica ????
  • Next by thread: Function Name from Inside the function