#Function Hierarchy?
21-Feb-92 01:49:49
Sb: #33911-#Function Hierarchy?
Fm: Tony Tanzillo [LISP TM] 71241,2067
To: Phillip E. Rutledge 70421,3012
Phil – In AutoLISP, there's no benefit to nesting function definitions. The
only reason that it is done in other LISP's is because it causes the execution
of the nested functions to be dynamically-scoped (e.g., a local declared in a
'parent' function can be referenced from the environment of a nested 'child'
function as well). But, since all scoping in AutoLISP is dynamic, you don't
need to nest definitions for *that* reason.
Another reason to nest definitions, is simply because they are specific to the
functions they are nested in, and are not needed outside of same. But, since
AutoLISP's error handling mechansim leaves the values of local symbols intact
when an error occurs, nesting functions to isolate them from other similarily-
named functions that might exist as globals, is basically futile.
In fact, when a function that has other functions nested in its definition as
declared locals, the bodies of the nested functions will exist in duplicate,
during execution of the parent. That is because when the parent executes, it
then assigns the nested definition to a local symbol, and there are then two
copies of the body of the nested function in memory (one is part of the parent
function's body, and one is assigned to the local symbol).
To make things worse, the first time an error occurs, the nested functions are
them made global, and the next time the parent function executes, there will be
*three* copies of the body of the nested function (the global created by the
error handler, which is pushed onto the stack frame each time the parent of the
nested function executes, makes three).
To make things even worse, if a function is recursive, and calls itself, there
will be one copy of each nested definition pushed onto the stack frame for each
degree of recursion.
So, I don't advocate this approach, partly because it imposes a lot of overhead
both memory, and performance wise, and it serves no purpose in an environment
where there is no real protection against the possiblity of a local variable's
value overwriting the value of a global variable with the same name.
-TonyT.