all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Chopping the last element of a list
       [not found] <fn8q5CKH0JRjvhUBk-weL0eqI0HA5QMHJ7ag-wmP2c_YXYavm1MNiga-Vz77nJCHECfOfW-UdvaTOH2nEAIopin5Nn7mXg2jgOxpVffs4So=@protonmail.com>
@ 2022-04-28  9:21 ` emacsq via Users list for the GNU Emacs text editor
  2022-04-28 21:33   ` Michael Heerdegen
  2022-04-28 23:52   ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 12+ messages in thread
From: emacsq via Users list for the GNU Emacs text editor @ 2022-04-28  9:21 UTC (permalink / raw)
  To: emacsq via Users list for the GNU Emacs text editor

If I want to do this then I can do, for example:

(prog1
(car (last mylist)) (setq mylist (nbutlast mylist)))

But here last and nbutlast walks the list twice unnecessarily.

Shouldn't emacs provide a function which does it in one step, so the list isn't walked twice?

E.g. (choplast mylist)

which returns a cons cell of (LAST . CHOPPEDLIST)

Of course, returning two values is not very lispy, but at least it could be more efficient.

Is there an existing function which does this in one step? If not, shouldn't there be one built-in emacs, for efficient manipulation of the list's end?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Chopping the last element of a list
@ 2022-04-28 21:27 Drew Adams
  2022-04-29  4:26 ` emacsq
  0 siblings, 1 reply; 12+ messages in thread
From: Drew Adams @ 2022-04-28 21:27 UTC (permalink / raw)
  To: 'emacsq'; +Cc: 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

[-- Attachment #1: Type: text/plain, Size: 1260 bytes --]

> If I want to do this then I can do, for example:
> 
> (prog1 (car (last mylist)) (setq mylist (nbutlast mylist)))
> 
> But here last and nbutlast walks the list twice unnecessarily.
> 
> Shouldn't emacs provide a function which does it in one step, so the
> list isn't walked twice?
> 
> E.g. (choplast mylist)
> 
> which returns a cons cell of (LAST . CHOPPEDLIST)
> 
> Of course, returning two values is not very lispy, but at least it
> could be more efficient.
> 
> Is there an existing function which does this in one step? If not,
> shouldn't there be one built-in emacs, for efficient manipulation of
> the list's end?

Here.  It returns the last element, and it
chops that last element off the list.

(You don't need to also return the updated
list, since you already have it as the arg.
But if you want to return it then return a
cons, as you did.)

(defun choplast (xs)
  (let ((m  (length xs)))
    (and (< 1 m)
         (let ((cons  (nthcdr (- m 2) xs)))
           (prog1 (cadr cons) (setcdr cons nil))))))

(setq foo  '(1 2 3 4 5 6 7 8 9))
(choplast foo) ; -> 9
 ; foo = (1 2 3 4 5 6 7 8)

(setq foo  '(1 2))
(choplast foo) ; -> 2
 ; foo = (1)

(setq foo  '(1))
(choplast foo) ; -> nil
 ; foo = (1)

[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 13876 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Chopping the last element of a list
  2022-04-28  9:21 ` emacsq via Users list for the GNU Emacs text editor
@ 2022-04-28 21:33   ` Michael Heerdegen
  2022-04-28 23:52   ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 12+ messages in thread
From: Michael Heerdegen @ 2022-04-28 21:33 UTC (permalink / raw)
  To: help-gnu-emacs

emacsq via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> If I want to do this then I can do, for example:
>
> (prog1
> (car (last mylist)) (setq mylist (nbutlast mylist)))
>
> But here last and nbutlast walks the list twice unnecessarily.

I would do that like

#+begin_src emacs-lisp
(let ((last2 (last my-list 2)))
  (prog1 (cons (cadr last2) my-list) (setcdr last2 nil)))
#+end_src

> Is there an existing function which does this in one step? If not,
> shouldn't there be one built-in emacs, for efficient manipulation of
> the list's end?

Dunno.  Does one need that so often?

I remember that I once implemented `rotate-left' and `rotate-right'
functions for lists, but I never used them.  Most of the time you need a
pointer to the last cdr more than once, and then it is more efficient to
remember that pointer in a variable.

What's your use case?

Michael.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Chopping the last element of a list
  2022-04-28  9:21 ` emacsq via Users list for the GNU Emacs text editor
  2022-04-28 21:33   ` Michael Heerdegen
@ 2022-04-28 23:52   ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 12+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-04-28 23:52 UTC (permalink / raw)
  To: help-gnu-emacs

emacsq via Users list for the GNU Emacs text editor [2022-04-28 09:21:23] wrote:
> If I want to do this then I can do, for example:
>
> (prog1
> (car (last mylist)) (setq mylist (nbutlast mylist)))
>
> But here last and nbutlast walks the list twice unnecessarily.

Right, so you see the problem with functions like `last` and `butlast`
which work on the end of a list: they are only useful if that's the
only thing you do with that list.  As soon as you try to do more, then
you end up traversing the list multiple times.

> Shouldn't emacs provide a function which does it in one step, so the
> list isn't walked twice?
>
> E.g. (choplast mylist)

That would just suffer from the same problem you just experienced
(i.e. everytime you need to do `choplast` plus something else, you'll
end up traversing the list twice).

Instead, every time you need to work on the end of a list, you should
start by getting a reference to that end (presumably with `last` with an
appropriate N) and then you work locally on that end.


        Stefan




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Chopping the last element of a list
@ 2022-04-29  1:02 Drew Adams
  0 siblings, 0 replies; 12+ messages in thread
From: Drew Adams @ 2022-04-29  1:02 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

[-- Attachment #1: Type: text/plain, Size: 812 bytes --]

> Right, so you see the problem with functions like `last` and `butlast`
> which work on the end of a list: they are only useful if that's the
> only thing you do with that list.  As soon as you try to do more, then
> you end up traversing the list multiple times.
> 
> > Shouldn't emacs provide a function which does it in one step, so the
> > list isn't walked twice? E.g. (choplast mylist)
> 
> That would just suffer from the same problem you just experienced
> (i.e. everytime you need to do `choplast` plus something else, you'll
> end up traversing the list twice).
> 
> Instead, every time you need to work on the end of a list, you should
> start by getting a reference to that end (presumably with `last` with
> an appropriate N) and then you work locally on that end.

+1.  Well put.

[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 13432 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Chopping the last element of a list
  2022-04-28 21:27 Chopping the last element of a list Drew Adams
@ 2022-04-29  4:26 ` emacsq
  2022-04-29  5:12   ` tomas
  2022-04-29 15:55   ` [External] : " Drew Adams
  0 siblings, 2 replies; 12+ messages in thread
From: emacsq @ 2022-04-29  4:26 UTC (permalink / raw)
  To: Drew Adams; +Cc: 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

>
> 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 ?



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Chopping the last element of a list
  2022-04-29  4:26 ` emacsq
@ 2022-04-29  5:12   ` tomas
  2022-04-29 15:55     ` [External] : " Drew Adams
  2022-04-29 15:55   ` [External] : " Drew Adams
  1 sibling, 1 reply; 12+ messages in thread
From: tomas @ 2022-04-29  5:12 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 585 bytes --]

On Fri, Apr 29, 2022 at 04:26:51AM +0000, emacsq wrote:
> >
> > 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?

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [External] : Re: Chopping the last element of a list
  2022-04-29  5:12   ` tomas
@ 2022-04-29 15:55     ` Drew Adams
  2022-04-30  5:25       ` tomas
  0 siblings, 1 reply; 12+ messages in thread
From: Drew Adams @ 2022-04-29 15:55 UTC (permalink / raw)
  To: tomas@tuxteam.de, help-gnu-emacs@gnu.org

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [External] : RE: Chopping the last element of a list
  2022-04-29  4:26 ` emacsq
  2022-04-29  5:12   ` tomas
@ 2022-04-29 15:55   ` Drew Adams
  1 sibling, 0 replies; 12+ messages in thread
From: Drew Adams @ 2022-04-29 15:55 UTC (permalink / raw)
  To: emacsq; +Cc: 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

> > 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 ?

I won't (I don't make changes to Emacs code).

But you can make any request you like, using
`M-x report-emacs-bug'.

I recommend you read Stefan's response to
your question, however.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [External] : Re: Chopping the last element of a list
  2022-04-29 15:55     ` [External] : " Drew Adams
@ 2022-04-30  5:25       ` tomas
  2022-04-30 14:47         ` Drew Adams
  0 siblings, 1 reply; 12+ messages in thread
From: tomas @ 2022-04-30  5:25 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs@gnu.org

[-- Attachment #1: Type: text/plain, Size: 1145 bytes --]

On Fri, Apr 29, 2022 at 03:55:39PM +0000, Drew Adams wrote:
> > > > 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.)

I know...

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

...and I know you know :)

I wanted to help OP to understand why changing things "in
place" has this special feel about it in Lisp land :-)

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [External] : Re: Chopping the last element of a list
  2022-04-30  5:25       ` tomas
@ 2022-04-30 14:47         ` Drew Adams
  2022-04-30 14:51           ` tomas
  0 siblings, 1 reply; 12+ messages in thread
From: Drew Adams @ 2022-04-30 14:47 UTC (permalink / raw)
  To: tomas@tuxteam.de; +Cc: help-gnu-emacs@gnu.org

> I wanted to help OP to understand why changing things "in
> place" has this special feel about it in Lisp land :-)

I know you did, and appreciate that. ;-)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [External] : Re: Chopping the last element of a list
  2022-04-30 14:47         ` Drew Adams
@ 2022-04-30 14:51           ` tomas
  0 siblings, 0 replies; 12+ messages in thread
From: tomas @ 2022-04-30 14:51 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs@gnu.org

[-- Attachment #1: Type: text/plain, Size: 289 bytes --]

On Sat, Apr 30, 2022 at 02:47:26PM +0000, Drew Adams wrote:
> > I wanted to help OP to understand why changing things "in
> > place" has this special feel about it in Lisp land :-)
> 
> I know you did, and appreciate that. ;-)

So we all know we know ;-D

Cheers & thanks
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-04-30 14:51 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-28 21:27 Chopping the last element of a list Drew Adams
2022-04-29  4:26 ` emacsq
2022-04-29  5:12   ` tomas
2022-04-29 15:55     ` [External] : " Drew Adams
2022-04-30  5:25       ` tomas
2022-04-30 14:47         ` Drew Adams
2022-04-30 14:51           ` tomas
2022-04-29 15:55   ` [External] : " Drew Adams
  -- strict thread matches above, loose matches on Subject: below --
2022-04-29  1:02 Drew Adams
     [not found] <fn8q5CKH0JRjvhUBk-weL0eqI0HA5QMHJ7ag-wmP2c_YXYavm1MNiga-Vz77nJCHECfOfW-UdvaTOH2nEAIopin5Nn7mXg2jgOxpVffs4So=@protonmail.com>
2022-04-28  9:21 ` emacsq via Users list for the GNU Emacs text editor
2022-04-28 21:33   ` Michael Heerdegen
2022-04-28 23:52   ` Stefan Monnier via Users list for the GNU Emacs text editor

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.