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

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

RE: [External] : Re: Chopping the last element of a list


From: Drew Adams
Subject: RE: [External] : Re: Chopping the last element of a list
Date: Fri, 29 Apr 2022 15:55:39 +0000

> > > Here. It returns the last element, and it
> > > chops that last element off the list.
> >
> > Thanks. It's quite simple and useful  to have along with
> > last/butlast/etc.
> >
> > Will you put it into subr.el, so emacs has it built-in ?
> 
> Read Drew's code again. Read the examples he provided.
> 
> Now, answer this one question: for a list with exactly
> one element, say
> 
>   (setq foo '(1))
> 
> what is its last element? What would you expect choplast
> to do? What does Drew's implementation do? Surprise?

Yes.  If you want to return 1 in that case then
be aware that there's no way to modify a cons
to turn it into nil.  (nil is a scalar - what
Lisp calls an "atom"; a cons is not.)

If you want behavior that returns 1 and sets some
variable whose value is (1) to nil, then you need
to pass a variable (that is, a symbol), not a list,
as the argument.

E.g., in that case you want behavior similar to
what `add-to-list' does: you pass it a _variable_
and it updates the variable's value.

I intentionally kept the same behavior as `butlast':
return nil and don't change anything for a list
with less than 2 elements.

reply via email to

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