Re: Scope Question
- From: Brian <quickbasicguru@xxxxxxxxx>
- Date: Tue, 29 Apr 2008 18:21:53 -0700 (PDT)
dstein64 wrote:
The problem is how do you tell a recursive call from a normal call?This counter function should give you an idea:
(let ((count 0))
� (defun counter ()
� � (incf count)))
(that is, make it a closure)
If your recursive function was in a LABELS, you would bind your
variable outside of the LABELS form. �Implementing the function with a
locally defined function would probably be best because it means you
might need to do less setup/cleanup.
I forgot to mention that I would like the variable to reset itself on
each call to the function (but not on the recursive calls. So in your
example, I would like count to be reset to 0 each time counter is
called from the program. Although there is no recursion in that
example, I would not like count to be reset each time counter is
called recursively by counter.
Perhaps you could do something like this: (although it isn't the best
example)
(defun my-length (list)
(let ((length 0))
(labels ((helper (list)
(when list
(incf length)
(helper (cdr list)))))
(helper list)
length)))
.
- References:
- Scope Question
- From: dstein64
- Re: Scope Question
- From: Brian
- Re: Scope Question
- From: dstein64
- Scope Question
- Prev by Date: Re: How to make a copy of a list
- Next by Date: Re: Scope Question
- Previous by thread: Re: Scope Question
- Next by thread: Re: Scope Question
- Index(es):
Relevant Pages
|