Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!cs.utexas.edu!tut.cis.ohio-state.edu!att!dptg!rutgers!aramis.rutgers.edu!bearcat.rutgers.edu!lou From: lou@bearcat.rutgers.edu (Lou Steinberg) Newsgroups: comp.lang.lisp Subject: Re: Simple question RE nested DEFUNs Message-ID: Date: 31 Jul 89 17:23:57 GMT References: <89Jul31.114325edt.11243@ephemeral.ai.toronto.edu> Organization: Rutgers Univ., New Brunswick, N.J. Lines: 43 To: bradb@ai.utoronto.ca Cc: lou In article <89Jul31.114325edt.11243@ephemeral.ai.toronto.edu> bradb@ai.utoronto.ca writes: > I come from a Scheme background, where I'm used to defining helping > functions within the body of the function being helped, as in > > (define (foo a) > > (define (bar b) > (... do some stuff with b and return result ...)) > > (... do some stuff with a and call bar as a ...) > (... helping function ...)) > I'm sure there is a proper way to do this in CommonLisp, but I'm > not sure what it is. Could some kind soul please enlighten me? See special forms flet and labels in the manual. I should point out that these constructs are used much less in lisp than the corresponding ones in languages like SCHEME or more traditional nested-scope languages. I speculate that this is at least partly because these constructs are harder to support properly in an interactive programming environment - e.g. to test these helper functions by calling them interactively you need a way to put yourself (i.e. the eval of your read-eval-print loop) into the "lexical" context where the helper function is defined. This can be implemented, but is a lot more complex than an environment that only lets you call non-nested definitions, and so has rarely if ever been implemented in lisp systems. (There are a number of other difficulties as well - e.g. implementing tracing.) Note also that the same kind of argument could be made about dynamic vs lexical scoping of variables, but in this case the various costs (e.g. in execution and debugging speeds) of dynamic scoping lead most lisp programmers (or at least those I teach :-) to use primarily lexical scoping for variables. Especially with proper use of packages, the cost of non-nested functions is much less than the cost of dynamic variables. -- Lou Steinberg uucp: {pretty much any major site}!rutgers!aramis.rutgers.edu!lou arpa: lou@aramis.rutgers.edu