[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tail recursion



A function like the following keeps blowing the control stack. 

(defun doit-list (list collector)
  (if (atom list) collector
      (doit-list (cdr list)
                 (cons (doit (car list))
                       collector))))

Is there a way to tell the lispm compiler that it's OK to turn the
tail recursion into iteration? [Let's assume I don't want to recode it
into a LOOP or MAPCAR or anything else.]

Ah. I have just found a mention of compiler:add-optimizer in Volume 4,
page 135 of the genera 7.2 manual. I can do what I
need with this, painfully. The following turns 
(doit-list x nil) into a LOOP.

(compiler:add-optimizer doit-list turn-doit-list-into-loop)
(defun turn-doit-list-into-loop (form)
   ;; form is a call to doit-list.
   (let ((arg1 (cadr form))
         (arg2 (caddr form)))
     (if (null arg2) `(loop for xyzzy in ,arg1 collect (doit xyzzy))
         form)))

I realize there are at least three bugs in that transformation. That's
one reason why I'd rather not use this feature if there's a more
reasonable way. Another is that I write a lot of functions similar to
doit-list, and don't want to recode the optimizer every time.

Any advice? Thanks in advance,

Larry Smith,
Computational Logic, Inc