From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: uzibalqa Newsgroups: gmane.emacs.help Subject: RE: [External] : Determine whether a list is an alist Date: Thu, 13 Jul 2023 15:57:55 +0000 Message-ID: References: <7d67c066-c4d7-a568-4dcf-ddfc6b1918fc@gmail.com> Mime-Version: 1.0 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="3062"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Platon Pronko , uzibalqa via Users list for the GNU Emacs text editor To: Drew Adams Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Jul 13 17:58:19 2023 Return-path: Envelope-to: geh-help-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 1qJyhu-0000a2-NR for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 13 Jul 2023 17:58:18 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qJyhh-0007oH-8z; Thu, 13 Jul 2023 11:58:05 -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 1qJyhg-0007o3-2n for help-gnu-emacs@gnu.org; Thu, 13 Jul 2023 11:58:04 -0400 Original-Received: from mail-40137.protonmail.ch ([185.70.40.137]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qJyhc-0004IC-GZ for help-gnu-emacs@gnu.org; Thu, 13 Jul 2023 11:58:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1689263877; x=1689523077; bh=gvY+XfiHmymrW3KnYfoyvfmPOX1jB+Rg8aG0V+0G2ew=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=moajDrkwkGaE9CIF+5uVoHRrWDuIhUnYZ0AxfI8cbIDJrczzo+AXEmnVkLw6A6F2C hDHP2IssooAhP4rowZMv3ng4RWS08702kF/KtLykg6WBSZz7p/GkfC4NGxLyw/KCPM e8oTr/xi5rXOtTpR9Js8aACBmuUP3b13Qx3QpFNNw5Vvzq9frjLfxI4zUlkBY5rlre F1VoFdkV5KrCjWjBMdIFrwWsabiCesjEcLI8oo45u/N33omG983lHdp8AbwQqMn5v5 /y4zlIA8FnjR/zSWngmNC/7+twVvxcDQW1yPUv8FNmXzSMcl5Qqj3msQcrGk+PaAk6 y5FQqQ3uk2YRA== In-Reply-To: Feedback-ID: 52887082:user:proton Received-SPF: pass client-ip=185.70.40.137; envelope-from=uzibalqa@proton.me; helo=mail-40137.protonmail.ch X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:144268 Archived-At: ------- Original Message ------- On Friday, July 14th, 2023 at 3:40 AM, Drew Adams w= rote: > > > Every list is an alist. > >=20 > > Can you elaborate? '(1 2 3) is a list, but I don't think it's an > > association list (at least according to Emacs manual - alist is suppose= d > > to be a list of cons cells, which '(1 2 3) definitely isn't). >=20 >=20 > Well, yes; and no. >=20 > "In Emacs Lisp, it is not an error if an element of an association list i= s not a cons cell. The alist search functions simply ignore such elements. = Many other versions of Lisp signal errors in such cases." (elisp) `Associat= ion Lists' >=20 > (let ((x 5)) > (assoc-default x '(1 2 3 4 5) nil x)) ; =3D> 5 >=20 >=20 > None of those list elements is a cons. >=20 > (assq 5 '(nil (5 . foo))) ; =3D> (5 . foo) >=20 >=20 > Element nil is not a cons. >=20 > ___ >=20 > The point is: specify what you really want when > asking to test whether a list is an alist. >=20 > Even asking whether a Lisp object is a list is > imprecise - proper/true list or just a nil or > cons? >=20 > Knowing what you're asking and what you really > want to ask is the first step (sometimes it's > even sufficient) toward getting the answer you > need. > ___ >=20 > And be aware that if you test every element of a > list then that can be costly. It might in some > cases be better to just test as you go, while > you're trying to do something with the alist - > e.g., while you're looking for an alist element > match. Agreed =20 > In many (most?) cases you don't really care > whether each element of the list is a cons - you > just want to retrieve the first match, and you > only want to traverse then entire list if you > have to (i.e., when there's no match or the last > element matches), and you don't want to traverse > it more than once. If I want to print the alist in (Key . Value) way, how would one skip extraneous (non-cons) list entries ?