Re: Scope Question



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.
.



Relevant Pages

  • Re: F8 and Yellow pattern for compile error
    ... I used *recursion* word as a pun. ... I wanted to *predict* when VBA would reset and when it wouldnt. ... >> recursive mode with VBA) to the code Line and herein lies my doubt. ...
    (microsoft.public.excel.programming)
  • Re: Scope Question
    ...   ... If your recursive function was in a LABELS, ... I forgot to mention that I would like the variable to reset itself on ... Although there is no recursion in that ...
    (comp.lang.lisp)
  • Re: Scope Question
    ... I would like count to be reset to 0 each time counter is ... Although there is no recursion in that ... (when (> times-left 0) ...
    (comp.lang.lisp)
  • hp50G and recursion
    ... I just bought this calculator, ... sometimes it ends up with e reset. ...
    (comp.sys.hp48)
  • Re: Cant resolve certain domains
    ... Server: svr-ad1.ppenet.com ... sun.com nameserver = cbru.br.ns.els-gms.att.net ... header flags: query, want recursion ...     HEADER: ...
    (microsoft.public.windows.server.dns)