Re: Scope Question
- From: Kaz Kylheku <kkylheku@xxxxxxxxx>
- Date: Tue, 29 Apr 2008 19:52:52 -0700 (PDT)
On Apr 29, 3:26 pm, Brian <quickbasicg...@xxxxxxxxx> wrote:
dstein64 wrote:
Suppose I have a recursive function that is searching for elements in
a tree. How can I store a list such that it is not reset on each
recursive call to the search function?
In general, how can I initialize a variable within a function that
will not be reset on each recursive call to that function? I hope my
question is clear. If not, please let me know. Thanks.
This counter function should give you an idea:
(let ((count 0))
(defun counter ()
(incf count)))
This might lead to a bad idea. Here, count is a de facto global
variable.
If you use a global variable for recursion context, then you will have
to use a lock to defend against any concurrency, or at the very least,
save and restore the variable to guard against any nested recursion
sessions (which can happen in non-concurrent programs).
This latter situation can rise, for instance, if you have a function
which recursively walks some data structure and (by means of the
recursion context) invokes a callback specified by the caller. The
callback may want to run a nested search using that same function.
.
- References:
- Scope Question
- From: dstein64
- Re: Scope Question
- From: Brian
- Scope Question
- Prev by Date: Re: Scope Question
- Next by Date: Re: How to make a copy of a list
- Previous by thread: Re: Scope Question
- Next by thread: Re: Scope Question
- Index(es):
Relevant Pages
|