unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Undefined behavior for the function ‘last’ or an error?
@ 2024-04-01 16:53 tpeplt
  2024-04-01 17:12 ` tpeplt
  2024-04-01 17:28 ` [External] : " Drew Adams
  0 siblings, 2 replies; 7+ messages in thread
From: tpeplt @ 2024-04-01 16:53 UTC (permalink / raw)
  To: help-gnu-emacs

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.

--



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

end of thread, other threads:[~2024-04-03  1:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-01 16:53 Undefined behavior for the function ‘last’ or an error? tpeplt
2024-04-01 17:12 ` 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

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