Let's say I run the following
(loop for i to 4 collect i)
Then I get a list (0 1 2 3 4)
. Now, if I want to append something to the result, I may use rplacd
on its last
element, but since Lisp lists are linked lists, it's not very efficient. Here the list is ridiculously small, but it's only an example.
However, since the loop facility returns the list in increasing order, it has to keep track of a pointer to the last element, and update the result with rplacd
, or something equivalent. A macroexpand-all
shows it's what CCL does, and probably other lisps too.
Question: Is there a way to use this "pointer" in the finally
clause? It would allow one to append something to the result, which is sometimes useful.
Of course, it's easy to code the pointer machinery, but it's not so nice. For example, the following will append the list e
to the list (0 1 ... n)
.
(defun foo (n e)
(let* ((a (list nil)) (tail a))
(loop for i to n
do (rplacd tail (setf tail (list i)))
finally (rplacd tail (setf tail e))
(return (cdr a)))))