From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#70155: 29.3; Several Emacs Lisp list functions accept non-list arguments Date: Sat, 27 Apr 2024 11:48:52 +0200 Message-ID: References: <87y19v8igf.fsf@gmail.com> <86ttjnw6z5.fsf@gnu.org> Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.15\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24314"; mail-complaints-to="usenet@ciao.gmane.io" Cc: tpeplt@gmail.com, Stefan Monnier , 70155@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Apr 27 11:50:09 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1s0eh6-00066Z-Px for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 27 Apr 2024 11:50:08 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s0egr-00045R-6Y; Sat, 27 Apr 2024 05:49:53 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s0ego-00044N-Ec for bug-gnu-emacs@gnu.org; Sat, 27 Apr 2024 05:49:50 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s0ego-0000NF-2n for bug-gnu-emacs@gnu.org; Sat, 27 Apr 2024 05:49:50 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1s0eh6-0004bu-JW for bug-gnu-emacs@gnu.org; Sat, 27 Apr 2024 05:50:08 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 27 Apr 2024 09:50:08 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70155 X-GNU-PR-Package: emacs Original-Received: via spool by 70155-submit@debbugs.gnu.org id=B70155.171421136817213 (code B ref 70155); Sat, 27 Apr 2024 09:50:08 +0000 Original-Received: (at 70155) by debbugs.gnu.org; 27 Apr 2024 09:49:28 +0000 Original-Received: from localhost ([127.0.0.1]:38968 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s0egQ-0004TK-A3 for submit@debbugs.gnu.org; Sat, 27 Apr 2024 05:49:28 -0400 Original-Received: from mail-lf1-x12f.google.com ([2a00:1450:4864:20::12f]:56401) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s0egJ-0004Qk-4X for 70155@debbugs.gnu.org; Sat, 27 Apr 2024 05:49:23 -0400 Original-Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-516d4d80d00so3867026e87.0 for <70155@debbugs.gnu.org>; Sat, 27 Apr 2024 02:49:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714211334; x=1714816134; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:sender:from:to:cc:subject :date:message-id:reply-to; bh=OFo5AOcZlPK9FwXsgeEw4HjqnMtIkh0uXmP0xCxc05w=; b=e2P5RzPM23/4cQHkMwvVB18vwa46Kkdj5nuvu/kiaxNkQ0ypre90ZnMFHHRoU/Rtge n99FAS0MODlq8nXs8OJ5GJ6GJb5F5cY2TTLr2PEfti8YjE3sjp9BXHfULjYb/BO/38Iw V4bAgQh2tYyJ+6EcxnCNcHtpZVaWplxjC7RCJk4VzfzxNv4zYGGNvcHS7ESZCbdBSZZH dBRcS8B2zj1esC5Sa76eg5UBAuKF9JI4KveDWoneiBW1YJqTpfbMwL/z45UCYe+1yts9 rxWzyNE/G3tDmOwRVoZuC0jppAxqC/fY/DDgeyiaAOrBDLZkumEs9KdzjeabO2l/ytRl njCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714211334; x=1714816134; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:sender:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=OFo5AOcZlPK9FwXsgeEw4HjqnMtIkh0uXmP0xCxc05w=; b=fyrdz3lSW6Hzi6WlTEvSO7iBXmmW8V0n/2uFcSll1nr9wXrxfcQ7gTgvpQmcTH+eOH vXBTYg8z0LZQ9DErIoWtmVqZmWMBIJ88vFGwfjQ8nO6YdK5BAijLf3G3R0aPDKWfclxP RCHAKmqt2gP/SL+vJkOpZ3CTpqlnO2Zvcw/Jnko5O/Tj9Jpe1mwx2evIKrx9xtsAUP+N Lg5YXS3w6MppXRIBySrAqjgiLI3GLHQ/NmqxOTdPgo2IBMgeIvGRyDuNgN4IF5EVLR56 g16e7Sjas8Uilky4LZiLqHYdDS1dgGU1aQgsOLeHFqY9YTK/cW/II6Njn1cLTdtmUGE5 efDg== X-Forwarded-Encrypted: i=1; AJvYcCV6otj/40S7IU6jWSrVFuFfSoDkY9gkNlZKmKr9YX/IyWuRQGtPqC9f0WkW/zVTkO5b3Qo9THr5Yso0nIiiGwka3KzJWWI= X-Gm-Message-State: AOJu0YxDoPpvEyRaaL7VEd15H1ePYlE9+OR1crrdJdj/oVd1HNTFB/1v ckGfTvmv3338U/ZGhDQypkYP60HbnqaLfPQQ2bJfZ3XZ2U8+f9iL X-Google-Smtp-Source: AGHT+IG41T24NzVpqnWiMgxUbjHKmbX+L9FN65TiIqUjlUFR/ekhcvKEoqmi6zgKdZTqOEXKwlhAdQ== X-Received: by 2002:a05:6512:3191:b0:51c:4d71:9690 with SMTP id i17-20020a056512319100b0051c4d719690mr3919428lfe.64.1714211333983; Sat, 27 Apr 2024 02:48:53 -0700 (PDT) Original-Received: from smtpclient.apple (c80-217-1-132.bredband.tele2.se. [80.217.1.132]) by smtp.gmail.com with ESMTPSA id o10-20020a198c0a000000b0051af630f9dcsm2371159lfd.189.2024.04.27.02.48.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 27 Apr 2024 02:48:53 -0700 (PDT) In-Reply-To: <86ttjnw6z5.fsf@gnu.org> X-Mailer: Apple Mail (2.3654.120.0.1.15) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:284033 Archived-At: 27 apr. 2024 kl. 10.22 skrev Eli Zaretskii : > 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: > Date: Tue, 02 Apr 2024 19:15:28 -0400 >=20 >> The built-in Emacs Lisp functions =E2=80=98last=E2=80=99, = =E2=80=98nthcdr=E2=80=99, =E2=80=98take=E2=80=99, >> and =E2=80=98ntake=E2=80=99 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) =3D (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.