Re: Scope Question



dstein64 wrote:
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.
The problem is how do you tell a recursive call from a normal call?

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



Relevant Pages

  • Re: how to define a local function
    ... them to be on the lookout for recursion, FLET means it isn't. ... Sorry for being picky, but it doesn't have to be recursion. ... Does anyone remember the historical reason for why _labels_ ...
    (comp.lang.lisp)
  • Re: how to define a local function
    ... them to be on the lookout for recursion, FLET means it isn't. ... Sorry for being picky, but it doesn't have to be recursion. ... Does anyone remember the historical reason for why _labels_ ...
    (comp.lang.lisp)
  • 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: how to define a local function
    ... them to be on the lookout for recursion, FLET means it isn't. ... Sorry for being picky, but it doesn't have to be recursion. ... Does anyone remember the historical reason for why _labels_ ...
    (comp.lang.lisp)