M. Janmaat wrote:
> Hi,
>
> What is the best way to remove the last item in a list...
>
> Now I'm doing this (reverse (cdr (reverse lst)))
>
> But it seems so stupid : )
Given the immutable lists AutoLISP has, that is actually a reasonable
way of doing that. (other lisps can also change list contents; in Common
Lisp there is the built-in function BUTLAST, which would do just what
you ask for.)
What is stupid is trying to do anything with the last item in a list. A
lisp list is always a singly-linked list, where the only access you have
to it is by following pointers starting from the first item.
So, you should always write your programs to preferentially operate on
the first item, instead of the last. Then adding an item is just one
CONS call, and removing an item is just one CDR call.
So, call REVERSE on your list once, and live with that end of the list
after that. If you really need the original order, call REVERSE again on
the final result, not for any intermediate phase.
If you really need to operate on both ends of a list, better build a
doubly-linked list and use that.
http://en.wikipedia.org/wiki/Linked_list
Some Lisp list manipulation for example:
http://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list-processing.html
http://www.gigamonkeys.com/book/beyond-lists-other-uses-for-cons-cells.html
(These are from a Common Lisp book, so there are operations not possible
in AutoLISP, but you get at least a little idea on normal usage.)
--