unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: tpeplt <tpeplt@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Undefined behavior for the function ‘last’ or an error?
Date: Mon, 01 Apr 2024 12:53:21 -0400	[thread overview]
Message-ID: <87il119g8u.fsf@gmail.com> (raw)

The built-in Emacs Lisp function ‘last’ accepts a list as its argument:

(last '(a b c)) => (c)

It also (surprisingly) accepts a non-list as its argument:

(last 'a) => a
(last 3.14) => 3.14
(last "string of chars") => "string of chars"

Contrast this with ‘butlast’ and ‘nbutlast’:

(butlast 'a)
*** Eval error ***  Wrong type argument: sequencep, a
(butlast 3.14)
*** Eval error ***  Wrong type argument: sequencep, 3.14
(butlast "string of chars")
*** Eval error ***  Wrong type argument: listp, "string of chars"

Is this behavior by ‘last’ an error, is it correct and undocumented, or
is it correct and documented in some obscure (to me) location?

Below are the docstring for ‘last’ and the entry in the elisp reference
manual.  Neither mentions this behavior by ‘last’.

Docstring for ‘last’:

(last LIST &optional N)

Return the last link of LIST.  Its car is the last element.
If LIST is nil, return nil.
If N is non-nil, return the Nth-to-last link of LIST.
If N is bigger than the length of LIST, return LIST.

From (info "(elisp) List Elements"):

 -- Function: last list &optional n
     This function returns the last link of LIST.  The ‘car’ of this
     link is the list’s last element.  If LIST is null, ‘nil’ is
     returned.  If N is non-‘nil’, the Nth-to-last link is returned
     instead, or the whole of LIST if N is bigger than LIST’s length.

--



             reply	other threads:[~2024-04-01 16:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-01 16:53 tpeplt [this message]
2024-04-01 17:12 ` Undefined behavior for the function ‘last’ or an error? tpeplt
2024-04-01 17:28 ` [External] : " Drew Adams
2024-04-01 18:35   ` tpeplt
2024-04-01 19:25     ` Drew Adams
2024-04-01 20:11       ` tpeplt
2024-04-03  1:05         ` tpeplt

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87il119g8u.fsf@gmail.com \
    --to=tpeplt@gmail.com \
    --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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).