all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Barry Margolin <barmar@alum.mit.edu>
To: help-gnu-emacs@gnu.org
Subject: Re: Reverse pop, return value
Date: Tue, 17 Nov 2009 16:34:53 -0500	[thread overview]
Message-ID: <barmar-A68984.16345317112009@nothing.attdns.com> (raw)
In-Reply-To: 9af6462e-4a2e-4325-b11d-9d8a6c2686b5@r24g2000yqd.googlegroups.com

In article 
<9af6462e-4a2e-4325-b11d-9d8a6c2686b5@r24g2000yqd.googlegroups.com>,
 LanX <lanx.perl@googlemail.com> wrote:

> Thanx Lennart!
> 
> Thats what I was looking for! 8)
> 
> ---------------
> (defun rpop (LIST)
> 	 "reverse pop"
> 	 (prog1
>             (car (last LIST))
>             (nbutlast LIST 1)
> 	 )
>        )
> --------------
> 
> bye
>   rolf

As I mentioned in the "remove last element in a list" thread, this won't 
work when the list gets down to 1 element.  nbutlast works by changing 
the cdr of the 2nd-to-last cons cell in the list to point to nil.  But 
if there's only 1 element, there's no 2nd-to-last cons cell to modify.  
You need to assign to the original variable, and this can only be done 
using a macro (that's why push and pop are macros).

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


  reply	other threads:[~2009-11-17 21:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-17 13:11 Remove last element in a list Nordlöw
2009-11-17 14:20 ` LanX
2009-11-17 15:32   ` Reverse pop, return value LanX
2009-11-17 15:48     ` Lennart Borgman
2009-11-17 16:53     ` David Kastrup
2009-11-17 17:33       ` LanX
     [not found]     ` <mailman.10897.1258472938.2239.help-gnu-emacs@gnu.org>
2009-11-17 17:20       ` LanX
2009-11-17 21:34         ` Barry Margolin [this message]
2009-11-18  0:28           ` LanX
2009-11-19 13:41             ` Barry Margolin
2009-11-17 21:26   ` Remove last element in a list Barry Margolin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=barmar-A68984.16345317112009@nothing.attdns.com \
    --to=barmar@alum.mit.edu \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.