3 ms·
> For performance reasons, the more conventional behavior in Lisp to collect values would be What you describe is what a human typical would write. For tools
by lispm 4y ago
> For performance reasons, the more conventional behavior in Lisp to collect values would be
What you describe is what a human typical would write.
For tools this is optimized. For example in (loop for e in list collect (oddp e)) this usually would be optimized. It would not add to the end and then reverse. Instead, it would keep a pointer to the end and add to the end via that pointer.
- kagevf 4y agoWould 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)))
- deleted 4y ago[deleted]