Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)



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


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


Thank you for the reply!

And I see that even in my original attempt, I could have used (setf (cdr)) instead of (rplacd)...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: