Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Removing the last item from a list

16 REPLIES 16
Reply
Message 1 of 17
Anonymous
6489 Views, 16 Replies

Removing the last item from a list

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

Kind regards
M
16 REPLIES 16
Message 2 of 17
Anonymous
in reply to: Anonymous

One way...

Command: (setq lst '(a b c))
(A B C)
Command: (vl-remove (last lst) lst)
(A B)


"M. Janmaat" wrote in message
news:5877552@discussion.autodesk.com...
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 : )

Kind regards
M
Message 3 of 17
Anonymous
in reply to: Anonymous

mmm...

Comando: (setq lst '(a c b c b c))
(A C B C B C)

Comando: (vl-remove (last lst) lst)
(A B B)


--

Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2008 <
(strcat "I like " (substr (ver) 8 4) "!")

--


"Joe Burke" ha scritto nel messaggio news:5877663@discussion.autodesk.com...
One way...

Command: (setq lst '(a b c))
(A B C)
Command: (vl-remove (last lst) lst)
(A B)
Message 4 of 17
Anonymous
in reply to: Anonymous

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

--
Message 5 of 17
Anonymous
in reply to: Anonymous

What you are doing is fine. Have you thought of restructuring your list so
that you are working with the head instead of the tail?
Strategies also vary depending on the function. For 2D points from 3D
points: (list (car lst)(cadr lst))


"M. Janmaat" wrote in message
news:5877552@discussion.autodesk.com...
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 : )

Kind regards
M
Message 6 of 17
Anonymous
in reply to: Anonymous

Doug,

I always use the head instead of the tail. Also in this case. This is just
an exeption where I need the tail...

I just thought there might be a (removelast lst) or something. But
apperently there isn't.

Thanks again for reply.

M


"Doug Broad" schreef in bericht
news:5877783@discussion.autodesk.com...
What you are doing is fine. Have you thought of restructuring your list so
that you are working with the head instead of the tail?
Strategies also vary depending on the function. For 2D points from 3D
points: (list (car lst)(cadr lst))


"M. Janmaat" wrote in message
news:5877552@discussion.autodesk.com...
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 : )

Kind regards
M
Message 7 of 17
Anonymous
in reply to: Anonymous

Thanx for the comment.

So far I'll stick to my solution.

See my reply at Doug aswell

M


"Martti Halminen" schreef in bericht
news:5877782@discussion.autodesk.com...
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.)

--
Message 8 of 17
Anonymous
in reply to: Anonymous

Thanks for the comment...

Exactly the problem I ran in to. : )

M

"Marc'Antonio Alessi" schreef in bericht
news:5877700@discussion.autodesk.com...
mmm...

Comando: (setq lst '(a c b c b c))
(A C B C B C)

Comando: (vl-remove (last lst) lst)
(A B B)


--

Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2008 <
(strcat "I like " (substr (ver) 8 4) "!")

--


"Joe Burke" ha scritto nel messaggio
news:5877663@discussion.autodesk.com...
One way...

Command: (setq lst '(a b c))
(A B C)
Command: (vl-remove (last lst) lst)
(A B)
Message 9 of 17
Anonymous
in reply to: Anonymous

In AutoLISP that's essentially how you do it.

But, if you are working with large lists and need to do this frequently against the same list, you will generate a lot of garbage, and should probably rethink how you store your data.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"M. Janmaat" wrote in message news:5877552@discussion.autodesk.com...
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 : )

Kind regards
M
Message 10 of 17
Anonymous
in reply to: Anonymous

May I suggest if that's, "exactly the problem I ran into..." then you should have
said so in your first post.

Joe Burke

"M. Janmaat" wrote in message
news:5877943@discussion.autodesk.com...
Thanks for the comment...

Exactly the problem I ran in to. : )

M

"Marc'Antonio Alessi" schreef in bericht
news:5877700@discussion.autodesk.com...
mmm...

Comando: (setq lst '(a c b c b c))
(A C B C B C)

Comando: (vl-remove (last lst) lst)
(A B B)


--

Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2008 <
(strcat "I like " (substr (ver) 8 4) "!")

--


"Joe Burke" ha scritto nel messaggio
news:5877663@discussion.autodesk.com...
One way...

Command: (setq lst '(a b c))
(A B C)
Command: (vl-remove (last lst) lst)
(A B)
Message 11 of 17
Anonymous
in reply to: Anonymous

"Joe Burke" wrote

>> May I suggest if that's, "exactly the problem I ran into..." then you should have
>> said so in your first post.

I don't get it. Why should he have told you that in his first post?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 12 of 17
Anonymous
in reply to: Anonymous

Me neither...

"Tony Tanzillo" schreef in bericht
news:5878238@discussion.autodesk.com...
"Joe Burke" wrote

>> May I suggest if that's, "exactly the problem I ran into..." then you
>> should have
>> said so in your first post.

I don't get it. Why should he have told you that in his first post?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 13 of 17
Anonymous
in reply to: Anonymous

Tony,

Seems to me common courtesy to let us know what was tried and did not work for
whatever reason in the original post.

Joe

"Tony Tanzillo" wrote in message
news:5878238@discussion.autodesk.com...
"Joe Burke" wrote

>> May I suggest if that's, "exactly the problem I ran into..." then you should have
>> said so in your first post.

I don't get it. Why should he have told you that in his first post?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 14 of 17
Anonymous
in reply to: Anonymous

[Not to put too find a point on it, but.... I agree that it would be helpful if someone describes
what they have tried that didn't work, so that others don't need to bother suggesting the same
thing. But there's really no reason to conclude M. was keeping people in the dark. It is certainly
possible that M. had *not* tried Joe's approach before the original post. Perhaps, in trying it
*after* Joe posted it, M. ran into the problem that Marc'Antonio discovered, Marc'Antonio was the
first to point out the difficulty, and M was simply confirming having also discovered it.]
--
Kent Cooper


"Joe Burke" wrote...
Tony,

Seems to me common courtesy to let us know what was tried and did not work for
whatever reason in the original post.

"Tony Tanzillo" wrote...
....
I don't get it. Why should he have told you that in his first post?
Message 15 of 17
Anonymous
in reply to: Anonymous

I didn't leave anyone in the dark, nor was it my intention...

"Kent Cooper" schreef in bericht
news:5880029@discussion.autodesk.com...
[Not to put too find a point on it, but.... I agree that it would be
helpful if someone describes
what they have tried that didn't work, so that others don't need to bother
suggesting the same
thing. But there's really no reason to conclude M. was keeping people in
the dark. It is certainly
possible that M. had *not* tried Joe's approach before the original post.
Perhaps, in trying it
*after* Joe posted it, M. ran into the problem that Marc'Antonio discovered,
Marc'Antonio was the
first to point out the difficulty, and M was simply confirming having also
discovered it.]
--
Kent Cooper


"Joe Burke" wrote...
Tony,

Seems to me common courtesy to let us know what was tried and did not work
for
whatever reason in the original post.

"Tony Tanzillo" wrote...
....
I don't get it. Why should he have told you that in his first post?
Message 16 of 17
Anonymous
in reply to: Anonymous

>> Seems to me common courtesy to let us know what was tried and did not work for whatever reason in the original post. <<

Sorry Joe, that was your error.

Trying to blame it on the OP isn't very becoming, if you ask me.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 17 of 17
Anonymous
in reply to: Anonymous

Hi Tony,

Are you turning introspective on us? 😉

Regards
Joe


"Tony Tanzillo" wrote in message
news:5882022@discussion.autodesk.com...
>> Seems to me common courtesy to let us know what was tried and did not work for
>> whatever reason in the original post. <<

Sorry Joe, that was your error.

Trying to blame it on the OP isn't very becoming, if you ask me.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost