This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Built-in Functions / Programming / Debugging  /
Stack

  • Stack[ ] shows the current evaluation stack, giving a list of the tags associated with evaluations that are currently being done.
  • Stack[ pattern ] gives a list of expressions currently being evaluated which match the pattern.
  • Stack[_] shows all expressions currently being evaluated.
  • You can call Stack from inside a dialog to see how the dialog was reached.
  • In the list returned by Stack[ pattern ], each expression is wrapped with HoldForm.
  • The maximum length of Stack[ ] is limited by $RecursionLimit.
  • Stack has attribute HoldFirst.
  • See the Mathematica book: Section 2.5.11.
  • See also: Trace.

    Further Examples

    Stack[_] gives the expressions that are being evaluated at the time when it is called, in this case including the Print function.

    In[1]:=

    {f[g[Print[Stack[_]]]]; , f[g[Print[Stack[_]]]], g[Print[Stack[_]]], Print[Stack[_]]}

    Stack gives the tags associated with the evaluations that are being done when it is called.

    In[2]:=

    {CompoundExpression, f, g, Print}

    Here is the standard recursive definition of the factorial function.

    In[3]:=

    This evaluates fac[10], starting a dialog when it encounters fac[4].

    In[4]:=

    TraceDialog::dgbgn: Entering Dialog; use Return[] to exit.

    Out[5]=

    This shows what objects were being evaluated when the dialog was started.

    In[6]:=

    Out[6]=

    This ends the dialog.

    In[7]:=

    TraceDialog::dgend: Exiting Dialog.

    Out[4]=

    StackInhibit prevents Print from being included on the stack.

    In[5]:=

    {CompoundExpression, f, g}

    StackBegin[ expr ] uses a fresh stack in the evaluation of expr.

    In[6]:=

    {g, h}

    Out[6]=