In order to demonstrate the effectiveness of tail recursion, I would like a way to access the depth of the call stack dynamically in Scheme.
Is there a way to do this? If not, is there a way to do this in other major functional languages (OCaml, Haskell, etc.)?
Racket allows you to store values in the call stack. You can use this to keep track of the depth. Here is how I would do it:
The output is:
The output shows that the depth doesn't increase in
foo
and that it does inbar
.