Given a recursive function, is there some simple way to display or store the value of recursion or "how many levels deep" the function is at a given time?
For instance, if we wrote the recursive function to calculate the factorial of a number (for any positive integer x):
r[x_] := If[x > 0, x*r[x - 1], 1]
Is there some way to output, as the function evaluates that it is a level "n"?
Given that the $RecursionLimit determines the max number of times a function can call itself nested, there must be some flag or value stored to keep track of how deep the current evaluation is that is compared to $RecursionLimit, that, if exceeded, aborts. What is this flag/value?
![f[5]](../../images/90b828472eb5e09d4186533b2b97c2c8.webp)
![aborted-f[24]](../../images/a9456f555884d290b9ed3375947df27e.webp)
![tail-f[24]](../../images/45b975f60e04c2dff05ce04a83d5d24e.webp)
Stackdocumentation: "The maximum length ofStack[]is limited by$RecursionLimit." – Oleksandr R. Jan 18 '15 at 01:26Length@Stack[]. I am not sure ifStack[]is really the most fundamental manifestation of the evaluation stack, but even if not, I think it should suffice. AddStackBegin/StackInhibitto taste. – Oleksandr R. Jan 18 '15 at 01:32$IterationLimitrather than$RecursionLimit. Sorry if I wasted your time because of that, but I think the link you found is useful. – Mr.Wizard Jan 18 '15 at 01:53Stack[]? I chose not to attempt this as I assumed you would. – Mr.Wizard Jan 21 '15 at 04:59Length@Stack[]is not what you are looking for? – Oleksandr R. Jan 25 '15 at 01:07