GNU bug report logs -
#70155
29.3; Several Emacs Lisp list functions accept non-list arguments
Previous Next
Reported by: <tpeplt <at> gmail.com>
Date: Tue, 2 Apr 2024 23:16:02 UTC
Severity: wishlist
Tags: wontfix
Found in version 29.3
Done: Stefan Kangas <stefankangas <at> gmail.com>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
27 apr. 2024 kl. 10.22 skrev Eli Zaretskii <eliz <at> gnu.org>:
> Mattias, Stefan: any comments on this? Should we document this, or
> should we change the code?
Eli, thank you for bringing this to our attention. I personally don't see much that we need to change.
Our priorities in Lisp are, arguably:
1. Well-defined behaviour should work as documented.
2. Undefined behaviour (ie, usage errors) should not cause crashes or corruption. This is what we usually call 'safety', or protecting the system's own abstractions.
3. Useful error conditions (such as file-not-found) should work as documented or at least be handled in a predictable and useful way.
4. The implementation should be efficient.
5. Incorrect usage should be detected and signalled as far as pragmatically possible, to help programmers find mistakes in their code.
The low ranking of the last item means that we don't necessarily promise to signal an error for every mistake a programmer can make.
However, in this case there are also algebraic motivations for the behaviour:
> From: <tpeplt <at> gmail.com>
> Date: Tue, 02 Apr 2024 19:15:28 -0400
>
>> The built-in Emacs Lisp functions ‘last’, ‘nthcdr’, ‘take’,
>> and ‘ntake’ are functions that accept lists as an argument.
>> However, they also accept non-list arguments without
>> signaling an error.
(nth 1 '(a b . c)) does not signal an error nor do we expect it to, so it's reasonable that
(take 2 '(a b . c)) -> (a b)
(take 1 '(a . b)) -> (a)
(take 0 'a) -> ()
do not signal errors either. nthcdr is the complement of [n]take and behaves the same way with respect to nonpositive arguments, and in fact has the definition
(nthcdr N L) = (cdr^N L)
for all naturals N, which means that
(nthcdr 0 'a) -> a
is correct.
(Of course if you ask me, I'd prefer it if lists were guaranteed to be proper, immutable, with the empty list an object distinct from the symbol nil and the false boolean value. Maybe next year.)
`last` works in the same way:
(last '(xN ... x1 . x0) M)
-> (last '(x{min(N,M)} ... x1 . x0) M)
with the base case
(last '(xN ... x1 . x0) 0) -> x0
ie,
(last x0 0) -> x0
for any atom x0.
Finally, [n]butlast are just [n]take with a different numeric argument, but here the special case
(butlast X 0) -> X
is motivated by performance considerations; it's more important to have it go in constant time.
This bug report was last modified 83 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.