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

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

Re: Check whether an object is an alist


From: Heime
Subject: Re: Check whether an object is an alist
Date: Tue, 17 Sep 2024 19:20:22 +0000

On Wednesday, September 18th, 2024 at 6:28 AM, tomas@tuxteam.de 
<tomas@tuxteam.de> wrote:

> On Tue, Sep 17, 2024 at 06:04:41PM +0000, Heime wrote:
> 
> > On Wednesday, September 18th, 2024 at 5:57 AM, tomas@tuxteam.de 
> > tomas@tuxteam.de wrote:
> 
> > > [...] as an exercise for the reader.
> > 
> > (cl-every #'consp '((foo . bar) nil (baz . quux)))
> 
> 
> Yes.
> --
> t

Have done this small test, and even though there is a nil (or anything 
else, e.g. jarod), assoc still works on the other keys.  This means that 
one can still be able to get correct values even if the object is not
a complete alist.  In a sense, if I am looking for keys in an object,
even though it is not an alist in a strict sense, things might still
work correctly.  Is this the reason there does not exist an alistp ? 
Or because traversing the list would be expensive ? 

But if we want to be strict that an alist is a list of cons cells 
with pairs of values, then only t1 should be accepted. 

(let ( (t1 '((foo . bar) (alice . bob) (baz . qux)))
       (t2 '((foo . bar) nil (alice . bob) (baz . qux)))
       (t3 '((foo . bar) jarod (alice . bob) (baz . qux))) )
  (message "%s" (assoc 'foo t1))
  (message "%s" (assoc 'alice t2))
  (message "%s" (assoc 'baz t3)) )

(foo . bar)
(alice . bob)
(baz . qux)




reply via email to

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