help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reverse pop, return value


From: Barry Margolin
Subject: Re: Reverse pop, return value
Date: Thu, 19 Nov 2009 08:41:00 -0500
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article 
<ef7158e9-63ef-4be8-bdca-72d0155a1943@c3g2000yqd.googlegroups.com>,
 LanX <lanx.perl@googlemail.com> wrote:

> Hi
> 
> On 17 Nov., 22:34, Barry Margolin <bar...@alum.mit.edu> wrote:
> > 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.  
> 
> Thx, so this is the way to go?
> 
> (defmacro rpop (LIST)
>          "reverse pop"
>          `(prog1
>             (car (last ,LIST))
>          (setq ,LIST (butlast ,LIST 1))
>          )
>        )

Yes.

> 
> > 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).
> 
> Hmm, for better understanding, could it be done with a defun if I pass
> a quoted symbol?
> 
> something like:
> 
> (rpop-symb 'list)

Yes, that would work.  That's why add-to-list requires a quoted symbol.

-- 
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 via email to

[Prev in Thread] Current Thread [Next in Thread]