all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: 'emacsq' <laszlomail@protonmail.com>
Cc: "'Help-Gnu-Emacs \(help-gnu-emacs@gnu.org\)'" <help-gnu-emacs@gnu.org>
Subject: RE: Chopping the last element of a list
Date: Thu, 28 Apr 2022 21:27:58 +0000	[thread overview]
Message-ID: <SJ0PR10MB548846462DC8D7C7F8A7CFB9F3FD9@SJ0PR10MB5488.namprd10.prod.outlook.com> (raw)

[-- 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 --]

             reply	other threads:[~2022-04-28 21:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-28 21:27 Drew Adams [this message]
2022-04-29  4:26 ` Chopping the last element of a list 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

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=SJ0PR10MB548846462DC8D7C7F8A7CFB9F3FD9@SJ0PR10MB5488.namprd10.prod.outlook.com \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=laszlomail@protonmail.com \
    /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.