MathGroup Archive 2006

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

Search the Archive

Re: Function Name from Inside the function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg71690] Re: [mg71639] Function Name from Inside the function
  • From: "Chris Chiasson" <chris at chiasson.name>
  • Date: Sun, 26 Nov 2006 03:49:30 -0500 (EST)
  • References: <200611251037.FAA15687@smc.vnet.net>

Functions:

Trace TracePrint TraceDialog

Options

TraceInternal, TraceForward, TraceBackward, TraceAbove

arguments:

an expression to trace
(optional) a pattern to watch for, which can drastically limit the
size of the trace output

Other Functions:
On / Off - used to track assignments to symbols

Using these tools, you too can come to hate evaluation chains and the stack.

On 11/25/06, Philipp <Philipp.M.O at gmail.com> wrote:
> 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.
>
>


-- 
http://chris.chiasson.name/


  • Prev by Date: Re: AGAIN Nested while loop!!!
  • Next by Date: RE: Plotting a function -
  • Previous by thread: Function Name from Inside the function
  • Next by thread: latex output and GraphicsArray