3 ms·
Would the code to maintain a pointer on the last element look something like this? (let* ((list (list 1 2 3 4)) (last (last list))) (rplacd last (li
by kagevf 4y ago
Would the code to maintain a pointer on the last element look something like this?
(let* ((list (list 1 2 3 4))
(last (last list)))
(rplacd last (list 5 6))
(setf last (last last))
(format t "list: ~a~%last: ~a~%" list last))
=> list: (1 2 3 4 5 6)
last: (6)
- lispm 4y agoin an iteration: (let* ((rlist (list '())) (last list)) (flet ((collect (item) (setf (cdr last) (list item) last (cdr last)) item) (finish () (cdr rlist))) (dotimes (i 10) (collect i)) (finish)))
- kagevf 4y agoThank you for the reply! And I see that even in my original attempt, I could have used (setf (cdr)) instead of (rplacd)...
- lispm 4y ago(let* ((rlist (list '())) (last rlist)) (flet ((collect (item &aux (cons-cell (list item))) (setf (cdr last) cons-cell last cons-cell) item) (finish () (cdr rlist))) (dotimes (i 10) (collect i)) (finish)))