help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [External] : Determine whether a list is an alist


From: Platon Pronko
Subject: Re: [External] : Determine whether a list is an alist
Date: Thu, 13 Jul 2023 18:49:51 +0300
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0

On 2023-07-13 18:40, Drew Adams wrote:
Every list is an alist.

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 supposed
to be a list of cons cells, which '(1 2 3) definitely isn't).

Well, yes; and no.

   "In Emacs Lisp, it is _not_ an error if an element of an association list is not 
a cons cell.  The alist search functions simply ignore such elements.  Many other 
versions of Lisp signal errors in such cases." (elisp) `Association Lists'

(let ((x  5))
   (assoc-default x '(1 2 3 4 5) nil x)) ; => 5

None of those list elements is a cons.

(assq 5 '(nil (5 . foo))) ; => (5 . foo)

Element nil is not a cons.

___

The point is: specify what you really want when
asking to test whether a list is an alist.

Even asking whether a Lisp object is a list is
imprecise - proper/true list or just a nil or
cons?

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

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.

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.

My apologies for not reading the manual thoroughly enough. Thanks for the 
explanation!

--
Best regards,
Platon Pronko
PGP 2A62D77A7A2CB94E




reply via email to

[Prev in Thread] Current Thread [Next in Thread]