The Directory Walker revisited

Dar Scott dsc at swcp.com
Tue Sep 2 13:41:00 EDT 2003


On Tuesday, September 2, 2003, at 06:33 AM, wouter wrote:

>> Are you saying the recursionLimit is so broken and or unhelpful that
>> you have to rewrite something as without recursion?
>
> Though recursion can be a very elegant way of programming, only use it 
> if you are sure the amount of recursion is not going to exceed its 
> limit .
> While using a try-catch structure is preventing the blocking by a 
> error dialog, it sure is no solution to be used in an app that is 
> meant to be used by others as it is not fulfilling its end.

This problem occurs whether recursion is used or not.

This is a long-time problem that is ignored by compiler writers and 
language designers.  Though hitting the stack limit is definitely 
behavior it is usually considered outside the scope of language 
semantics.

Even when a language designer says tail recursion optimization is used, 
that does not mean much in general.

If recursion is not used, a compiler might be able to estimate the 
needed stack.  I don't know of one that does.  A debugger might fill a 
stack space with some code so that the max stack depth can be reported, 
but this is harder in modern memory management system especially with 
the stack space growing and shrinking.  Another method is to check on 
the depth once in a while.

In general all one can do is test a lot with test cases that might 
maximize the stack for some setting and then set it even higher.

If recursion depth depends on outside data, then a either a depth test 
is needed or a try-catch.  As you mentioned, neither does the job.  
However, if the maximum is very high, few would care.  Some folks might 
prefer an error that says "Maximum folder depth of 1000 exceeded" 
rather than "too complicated" or "stack limit reached", so depth 
checking might be better.  Even then a high stack size value is needed. 
  An alternative to recursion depth checking is range checking in a 
wrapper function.

Modern hardware and OS's adapt to this by allowing the stack space to 
grow and shrink.

As much as I prefer recursion in simplicity and ease of programming and 
thus the creation of more reliable code, I have to agree that use of 
repeat would be less likely to blow the stack with, uh, interesting 
data.  Even so, many 'repeat' methods would require keeping lists that 
would eventually hit memory limits anyway.

I think we are in this mess because schools produce unix-worshiping C 
programmers instead of computer scientists.  There was a greater 
advance in CS in the quarter century _before_ electronic computers than 
there has been in the last quarter century.

Dar Scott




More information about the use-livecode mailing list