all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Determine whether a list is an alist
@ 2023-07-13 10:40 uzibalqa
  2023-07-13 14:31 ` [External] : " Drew Adams
  0 siblings, 1 reply; 15+ messages in thread
From: uzibalqa @ 2023-07-13 10:40 UTC (permalink / raw)
  To: uzibalqa via Users list for the GNU Emacs text editor

Is there some test that determines whether a list is an alist?





^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 10:40 Determine whether a list is an alist uzibalqa
@ 2023-07-13 14:31 ` Drew Adams
  2023-07-13 14:40   ` Platon Pronko
  2023-07-13 14:42   ` uzibalqa
  0 siblings, 2 replies; 15+ messages in thread
From: Drew Adams @ 2023-07-13 14:31 UTC (permalink / raw)
  To: uzibalqa, uzibalqa via Users list for the GNU Emacs text editor

> Is there some test that determines whether a list is an alist?

Every list is an alist.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [External] : Determine whether a list is an alist
  2023-07-13 14:31 ` [External] : " Drew Adams
@ 2023-07-13 14:40   ` Platon Pronko
  2023-07-13 15:40     ` Drew Adams
  2023-07-13 14:42   ` uzibalqa
  1 sibling, 1 reply; 15+ messages in thread
From: Platon Pronko @ 2023-07-13 14:40 UTC (permalink / raw)
  To: Drew Adams, uzibalqa,
	uzibalqa via Users list for the GNU Emacs text editor

On 2023-07-13 17:31, Drew Adams wrote:
>> Is there some test that determines whether a list is an alist?
> 
> 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).



^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 14:31 ` [External] : " Drew Adams
  2023-07-13 14:40   ` Platon Pronko
@ 2023-07-13 14:42   ` uzibalqa
  1 sibling, 0 replies; 15+ messages in thread
From: uzibalqa @ 2023-07-13 14:42 UTC (permalink / raw)
  To: Drew Adams; +Cc: uzibalqa via Users list for the GNU Emacs text editor

------- Original Message -------
On Friday, July 14th, 2023 at 2:31 AM, Drew Adams <drew.adams@oracle.com> wrote:


> > Is there some test that determines whether a list is an alist?
> 
> 
> Every list is an alist.

The manual distinguishes an alist as a list of cons cells called associations - where
the CAR of each cons cell is the key, and the CDR is the associated value.





^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 14:40   ` Platon Pronko
@ 2023-07-13 15:40     ` Drew Adams
  2023-07-13 15:44       ` Drew Adams
                         ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: Drew Adams @ 2023-07-13 15:40 UTC (permalink / raw)
  To: Platon Pronko, uzibalqa,
	uzibalqa via Users list for the GNU Emacs text editor

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 15:40     ` Drew Adams
@ 2023-07-13 15:44       ` Drew Adams
  2023-07-13 15:49       ` Platon Pronko
                         ` (2 subsequent siblings)
  3 siblings, 0 replies; 15+ messages in thread
From: Drew Adams @ 2023-07-13 15:44 UTC (permalink / raw)
  To: Drew Adams, Platon Pronko, uzibalqa,
	uzibalqa via Users list for the GNU Emacs text editor

> you only want to traverse then entire list if
                              ^
                             the

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [External] : Determine whether a list is an alist
  2023-07-13 15:40     ` Drew Adams
  2023-07-13 15:44       ` Drew Adams
@ 2023-07-13 15:49       ` Platon Pronko
  2023-07-13 15:56         ` Drew Adams
  2023-07-13 15:57       ` uzibalqa
  2023-07-13 15:59       ` Drew Adams
  3 siblings, 1 reply; 15+ messages in thread
From: Platon Pronko @ 2023-07-13 15:49 UTC (permalink / raw)
  To: Drew Adams, uzibalqa,
	uzibalqa via Users list for the GNU Emacs text editor

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




^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 15:49       ` Platon Pronko
@ 2023-07-13 15:56         ` Drew Adams
  0 siblings, 0 replies; 15+ messages in thread
From: Drew Adams @ 2023-07-13 15:56 UTC (permalink / raw)
  To: Platon Pronko, uzibalqa,
	uzibalqa via Users list for the GNU Emacs text editor

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

No need to apologize.  Your question helps,
probably more than my (first) answer, which
was maybe in some ways a "trick" question -
but only "trick" in the sense that Elisp is
tricky - loose - when it comes to alists.

The devil is in the details, and in a user's
intention.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 15:40     ` Drew Adams
  2023-07-13 15:44       ` Drew Adams
  2023-07-13 15:49       ` Platon Pronko
@ 2023-07-13 15:57       ` uzibalqa
  2023-07-13 16:06         ` Platon Pronko
  2023-07-13 16:07         ` Drew Adams
  2023-07-13 15:59       ` Drew Adams
  3 siblings, 2 replies; 15+ messages in thread
From: uzibalqa @ 2023-07-13 15:57 UTC (permalink / raw)
  To: Drew Adams
  Cc: Platon Pronko,
	uzibalqa via Users list for the GNU Emacs text editor


------- Original Message -------
On Friday, July 14th, 2023 at 3:40 AM, Drew Adams <drew.adams@oracle.com> 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.

Agreed
 
> 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 ?



^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 15:40     ` Drew Adams
                         ` (2 preceding siblings ...)
  2023-07-13 15:57       ` uzibalqa
@ 2023-07-13 15:59       ` Drew Adams
  3 siblings, 0 replies; 15+ messages in thread
From: Drew Adams @ 2023-07-13 15:59 UTC (permalink / raw)
  To: Drew Adams, Platon Pronko, uzibalqa,
	uzibalqa via Users list for the GNU Emacs text editor

Oh, and BTW, I just noticed this:
https://emacs.stackexchange.com/q/77969

Unclear question.  Knowing what you really
want is the first step to asking for help.

And yes, it's not always easy to know what
you really want (including what you want
to know).  That's why figuring that out is
the first step (and sometimes the only one)
to finding the answer.

Ask yourself what you're trying to do, and
why.  I strongly recommend this little gem
of a book by George Polya (1945):

https://en.wikipedia.org/wiki/How_to_Solve_It

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [External] : Determine whether a list is an alist
  2023-07-13 15:57       ` uzibalqa
@ 2023-07-13 16:06         ` Platon Pronko
  2023-07-13 16:14           ` Drew Adams
  2023-07-13 16:07         ` Drew Adams
  1 sibling, 1 reply; 15+ messages in thread
From: Platon Pronko @ 2023-07-13 16:06 UTC (permalink / raw)
  To: uzibalqa, Drew Adams
  Cc: uzibalqa via Users list for the GNU Emacs text editor

On 2023-07-13 18:57, uzibalqa wrote:
> If I want to print the alist in (Key . Value) way, how would one skip
> extraneous (non-cons) list entries ?

There's a consp function that you can use to check if entry is cons or not. (this page is the first result on google for me: https://www.gnu.org/software/emacs/manual/html_node/elisp/List_002drelated-Predicates.html)

-- 
Best regards,
Platon Pronko
PGP 2A62D77A7A2CB94E




^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 15:57       ` uzibalqa
  2023-07-13 16:06         ` Platon Pronko
@ 2023-07-13 16:07         ` Drew Adams
  2023-07-13 16:12           ` Robert Pluim
  1 sibling, 1 reply; 15+ messages in thread
From: Drew Adams @ 2023-07-13 16:07 UTC (permalink / raw)
  To: uzibalqa
  Cc: Platon Pronko,
	uzibalqa via Users list for the GNU Emacs text editor

> > 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 ?

`dolist' is your friend.

Printing is a side-effect thingie.  `dolist' is
a natural for iterating over a list, performing
side effects.  Just test each list element and
do what you want with it - that can include
doing nothing with it.  "Extraneous" is in the
eye of the caller-beholder.

This is no different from C, Fortran, assembler,
etc., except that Lisp gives you easy to use
lists and list iterators, ready-made.

LISP: LISt Processing.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [External] : Determine whether a list is an alist
  2023-07-13 16:07         ` Drew Adams
@ 2023-07-13 16:12           ` Robert Pluim
  2023-07-13 16:16             ` Drew Adams
  0 siblings, 1 reply; 15+ messages in thread
From: Robert Pluim @ 2023-07-13 16:12 UTC (permalink / raw)
  To: Drew Adams
  Cc: uzibalqa, Platon Pronko,
	uzibalqa via Users list for the GNU Emacs text editor

>>>>> On Thu, 13 Jul 2023 16:07:49 +0000, Drew Adams <drew.adams@oracle.com> said:
    Drew> This is no different from C, Fortran, assembler,
    Drew> etc., except that Lisp gives you easy to use
    Drew> lists and list iterators, ready-made.

Theyʼre easy to use, but theyʼre always in the reverse order ;-)

Robert
-- 



^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 16:06         ` Platon Pronko
@ 2023-07-13 16:14           ` Drew Adams
  0 siblings, 0 replies; 15+ messages in thread
From: Drew Adams @ 2023-07-13 16:14 UTC (permalink / raw)
  To: Platon Pronko, uzibalqa
  Cc: uzibalqa via Users list for the GNU Emacs text editor

> There's a consp function that you can use to check
> if entry is cons or not.

And there's its complement, `atom'.
An old-school name, but it says what
it means: "atomic", i.e., indivisible.

But it's only atomic wrt lists.  Lisp
now has "atomic" objects that are not
lists but that are in some way divisible.
E.g., a string and a vector are `atom'ic
(they're not `consp'), but you can get
inside them.

So yeah, nowadays `consp' says better
what it means than `atom' does.  You
have to read `atom' as just not-a-cons.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [External] : Determine whether a list is an alist
  2023-07-13 16:12           ` Robert Pluim
@ 2023-07-13 16:16             ` Drew Adams
  0 siblings, 0 replies; 15+ messages in thread
From: Drew Adams @ 2023-07-13 16:16 UTC (permalink / raw)
  To: Robert Pluim
  Cc: uzibalqa, Platon Pronko,
	uzibalqa via Users list for the GNU Emacs text editor

>     Drew> This is no different from C, Fortran, assembler,
>     Drew> etc., except that Lisp gives you easy to use
>     Drew> lists and list iterators, ready-made.
> 
> Theyʼre easy to use, but theyʼre always in the reverse order ;-)

Depends where you start and which way you head.
And whether or not you're in MirrorLand.

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-07-13 16:16 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-13 10:40 Determine whether a list is an alist uzibalqa
2023-07-13 14:31 ` [External] : " Drew Adams
2023-07-13 14:40   ` Platon Pronko
2023-07-13 15:40     ` Drew Adams
2023-07-13 15:44       ` Drew Adams
2023-07-13 15:49       ` Platon Pronko
2023-07-13 15:56         ` Drew Adams
2023-07-13 15:57       ` uzibalqa
2023-07-13 16:06         ` Platon Pronko
2023-07-13 16:14           ` Drew Adams
2023-07-13 16:07         ` Drew Adams
2023-07-13 16:12           ` Robert Pluim
2023-07-13 16:16             ` Drew Adams
2023-07-13 15:59       ` Drew Adams
2023-07-13 14:42   ` uzibalqa

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.