[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Undefined behavior for the function ‘last’ or an error?
From: |
tpeplt |
Subject: |
Undefined behavior for the function ‘last’ or an error? |
Date: |
Mon, 01 Apr 2024 12:53:21 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
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.
--
- Undefined behavior for the function ‘last’ or an error?,
tpeplt <=