unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattias.engdegard@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: tpeplt@gmail.com, Stefan Monnier <monnier@iro.umontreal.ca>,
	70155@debbugs.gnu.org
Subject: bug#70155: 29.3; Several Emacs Lisp list functions accept non-list arguments
Date: Sat, 27 Apr 2024 11:48:52 +0200	[thread overview]
Message-ID: <F6D3B900-CB03-40CA-8BBD-7CA592EFC9DB@gmail.com> (raw)
In-Reply-To: <86ttjnw6z5.fsf@gnu.org>

27 apr. 2024 kl. 10.22 skrev Eli Zaretskii <eliz@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@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.






  reply	other threads:[~2024-04-27  9:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-02 23:15 bug#70155: 29.3; Several Emacs Lisp list functions accept non-list arguments tpeplt
2024-04-03  2:55 ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-27  8:22 ` Eli Zaretskii
2024-04-27  9:48   ` Mattias Engdegård [this message]
2024-04-27 15:01     ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-27 14:38   ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors

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=F6D3B900-CB03-40CA-8BBD-7CA592EFC9DB@gmail.com \
    --to=mattias.engdegard@gmail.com \
    --cc=70155@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=tpeplt@gmail.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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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