unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* lexicographic list comparison
@ 2022-09-09 19:27 Sam Steingold
  2022-09-10  9:07 ` Mattias Engdegård
  0 siblings, 1 reply; 10+ messages in thread
From: Sam Steingold @ 2022-09-09 19:27 UTC (permalink / raw)
  To: emacs-devel

Hi,

What do you do when sorting a list of lists of numbers?

There does not appear to be a standard comparison function like
--8<---------------cut here---------------start------------->8---
(defun lexicographic-compare-lists (l1 l2 &optional lessp)
  "lexicographic list comparison"
  (unless lessp
    (setq lessp #'<))
  (or (null l2) (null l1)
      (funcall lessp (car l1) (car l2))
      (and (not (funcall lessp (car l2) (car l1)))
           (lexicographic-compare-lists (cdr l1) (cdr l2) lessp))))
--8<---------------cut here---------------end--------------->8---

[[[It appears that it is less critical for strings because one can do
--8<---------------cut here---------------start------------->8---
(sort list-of-lists-of-strings
      (lambda (l1 l2) (string< (string-join l1) (string-join l2))))
--8<---------------cut here---------------end--------------->8---
instead of
--8<---------------cut here---------------start------------->8---
(sort list-of-lists-of-strings
      (lambda (l1 l2) (lexicographic-compare-lists l1 l2 #'string<)))
--8<---------------cut here---------------end--------------->8---
even though it conses for each comparison(!)]]]

Or maybe sorting lists of lists is just such a rare op that no one has
ever encountered it before me? ;-)

Thank you.

-- 
Sam Steingold (https://aphar.dreamwidth.org/) on darwin Ns 10.3.2113
https://lastingimpactpsychology.com https://steingoldpsychology.com
https://ij.org/ https://honestreporting.com https://memri.org
You do not have time or money to sue anyone rich enough to be worth suing.



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

* Re: lexicographic list comparison
  2022-09-09 19:27 lexicographic list comparison Sam Steingold
@ 2022-09-10  9:07 ` Mattias Engdegård
  2022-09-10  9:22   ` Emanuel Berg
                     ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Mattias Engdegård @ 2022-09-10  9:07 UTC (permalink / raw)
  To: sds; +Cc: emacs-devel

9 sep. 2022 kl. 21.27 skrev Sam Steingold <sds@gnu.org>:

> What do you do when sorting a list of lists of numbers?

Sigh deeply and write ad-hoc code for the nth time.

> Or maybe sorting lists of lists is just such a rare op that no one has
> ever encountered it before me?

It would be useful to have a total ordering on Lisp values: for heterogeneous ordered collections, for simplifying multi-key sorting, for normalising unordered collections, etc.

The devil is in the details: are strings compared with our without properties? Do we observe the wonky IEEE rules for ordering floats? Is there a particularly useful ordering between elements of different types? Do we need to worry about circularity? How do we compare hash tables? And so on.

Consistency with `equal` would be desirable (up to hash table identity, perhaps).




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

* Re: lexicographic list comparison
  2022-09-10  9:07 ` Mattias Engdegård
@ 2022-09-10  9:22   ` Emanuel Berg
  2022-09-10  9:26   ` tomas
  2022-09-12 14:45   ` Sam Steingold
  2 siblings, 0 replies; 10+ messages in thread
From: Emanuel Berg @ 2022-09-10  9:22 UTC (permalink / raw)
  To: emacs-devel

Mattias Engdegård wrote:

>> Or maybe sorting lists of lists is just such a rare op that
>> no one has ever encountered it before me?
>
> It would be useful to have a total ordering on Lisp values:
> for heterogeneous ordered collections, for simplifying
> multi-key sorting, for normalising unordered
> collections, etc.
>
> The devil is in the details: are strings compared with our
> without properties? Do we observe the wonky IEEE rules for
> ordering floats? Is there a particularly useful ordering
> between elements of different types? Do we need to worry
> about circularity? How do we compare hash tables? And so on.
>
> Consistency with `equal` would be desirable (up to hash
> table identity, perhaps).

Interesting, what is this, type theory?

Sounds like something that is available in Haskell maybe ...

What is the benefit if you have it? Sort things recursively
and with no fear running into data of a type that would
wreck it?

It's easy to do, (order-pair a b) which accepts any an all
data and returns the list (a b) iff a < b. It could first
check types, then delegate the task to the existing
greater-than function for all cases that ... makes sense.

Making up rules that makes sense for everything that don't
make sense to compare is maybe more difficult ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: lexicographic list comparison
  2022-09-10  9:07 ` Mattias Engdegård
  2022-09-10  9:22   ` Emanuel Berg
@ 2022-09-10  9:26   ` tomas
  2022-09-12 14:45   ` Sam Steingold
  2 siblings, 0 replies; 10+ messages in thread
From: tomas @ 2022-09-10  9:26 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1109 bytes --]

On Sat, Sep 10, 2022 at 11:07:49AM +0200, Mattias Engdegård wrote:
> 9 sep. 2022 kl. 21.27 skrev Sam Steingold <sds@gnu.org>:
> 
> > What do you do when sorting a list of lists of numbers?
> 
> Sigh deeply and write ad-hoc code for the nth time.

:-)

> It would be useful to have a total ordering on Lisp values: for heterogeneous ordered collections, for simplifying multi-key sorting, for normalising unordered collections, etc.
> 
> The devil is in the details: are strings compared with our without properties? Do we observe the wonky IEEE rules for ordering floats? Is there a particularly useful ordering between elements of different types? Do we need to worry about circularity? How do we compare hash tables? And so on.
> 

And language-dependent collation, and Unicode equivalence, and NaNs
and...

Details, indeed :-)

A look at the troubles and tribulations a DBMS goes through gives one
a rough idea on how interesting a general solution can become...

> Consistency with `equal` would be desirable (up to hash table identity, perhaps).

This, too :)

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: lexicographic list comparison
  2022-09-10  9:07 ` Mattias Engdegård
  2022-09-10  9:22   ` Emanuel Berg
  2022-09-10  9:26   ` tomas
@ 2022-09-12 14:45   ` Sam Steingold
  2022-09-12 17:53     ` mattiase
  2 siblings, 1 reply; 10+ messages in thread
From: Sam Steingold @ 2022-09-12 14:45 UTC (permalink / raw)
  To: emacs-devel, Mattias Engdegård

> * Mattias Engdegård <znggvnfr@npz.bet> [2022-09-10 11:07:49 +0200]:
>
> 9 sep. 2022 kl. 21.27 skrev Sam Steingold <sds@gnu.org>:
>
>> What do you do when sorting a list of lists of numbers?
>
> Sigh deeply and write ad-hoc code for the nth time.

okay, so, I suppose, you find the `lexicographic-compare-lists' from TS
useful, right?

>> Or maybe sorting lists of lists is just such a rare op that no one has
>> ever encountered it before me?
>
> It would be useful to have a total ordering on Lisp values: for
> heterogeneous ordered collections, for simplifying multi-key sorting,
> for normalising unordered collections, etc.

I certainly have no intention of comparing strings with numbers &c.
My question was about a list of _homogeneous_ lists, and comparing to,
say, lists of numbers, is done lexicographically based on number
comparison.


-- 
Sam Steingold (https://aphar.dreamwidth.org/) on darwin Ns 10.3.2113
https://lastingimpactpsychology.com https://steingoldpsychology.com
https://www.peaceandtolerance.org/ https://www.memritv.org
When C++ is your hammer, everything looks like a thumb.



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

* Re: lexicographic list comparison
  2022-09-12 14:45   ` Sam Steingold
@ 2022-09-12 17:53     ` mattiase
  2022-09-12 18:31       ` Stefan Monnier
  2022-09-12 18:35       ` Sam Steingold
  0 siblings, 2 replies; 10+ messages in thread
From: mattiase @ 2022-09-12 17:53 UTC (permalink / raw)
  To: sds; +Cc: emacs-devel

12 sep. 2022 kl. 16.45 skrev Sam Steingold <sds@gnu.org>:

> okay, so, I suppose, you find the `lexicographic-compare-lists' from TS
> useful, right?

(What is TS in this context?)
I usually just copy-paste code from an older project of mine (who doesn't?) because that way I know that it works, how it works, what its performance is like, etc.

> I certainly have no intention of comparing strings with numbers &c.
> My question was about a list of _homogeneous_ lists, and comparing to,
> say, lists of numbers, is done lexicographically based on number
> comparison.

We could certainly add comparison functions for lists, then ones for arrays, then for hash tables, and so on. Or just write one that works for all built-in types and impose an ordering between objects of distinct types. It would be more useful (if harder to write).





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

* Re: lexicographic list comparison
  2022-09-12 17:53     ` mattiase
@ 2022-09-12 18:31       ` Stefan Monnier
  2022-09-12 18:41         ` Sam Steingold
  2022-09-12 18:35       ` Sam Steingold
  1 sibling, 1 reply; 10+ messages in thread
From: Stefan Monnier @ 2022-09-12 18:31 UTC (permalink / raw)
  To: mattiase; +Cc: sds, emacs-devel

>> I certainly have no intention of comparing strings with numbers &c.
>> My question was about a list of _homogeneous_ lists, and comparing to,
>> say, lists of numbers, is done lexicographically based on number
>> comparison.
>
> We could certainly add comparison functions for lists, then ones for arrays,
> then for hash tables, and so on. Or just write one that works for all
> built-in types and impose an ordering between objects of distinct types. It
> would be more useful (if harder to write).

We could write a `cl-defgeneric` with a few basic instances.

Another option is to provide combinators: e.g. provide a `list-compare`
function which takes the comparison function to use for the elements and
return a comparison function that works of lists of such elements, so
you can do things like:

    (sort foo (list-compare (list-compare #'<)))

[ Tho I suspect you'll want those comparison functions to return
  a -1/0/1 result rather than just a boolean, so you'd need an extra
  combinator to turn such a comparison function into the kind expected
  by `sort`.  ]


-- Stefan




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

* Re: lexicographic list comparison
  2022-09-12 17:53     ` mattiase
  2022-09-12 18:31       ` Stefan Monnier
@ 2022-09-12 18:35       ` Sam Steingold
  1 sibling, 0 replies; 10+ messages in thread
From: Sam Steingold @ 2022-09-12 18:35 UTC (permalink / raw)
  To: emacs-devel, mattiase

> *  <znggvnfr@npz.bet> [2022-09-12 19:53:42 +0200]:
>
> 12 sep. 2022 kl. 16.45 skrev Sam Steingold <sds@gnu.org>:
>
>> okay, so, I suppose, you find the `lexicographic-compare-lists' from TS
>> useful, right?
>
> (What is TS in this context?)

TS=topic start (the initial message in the thread)

> I usually just copy-paste code from an older project of mine (who
> doesn't?) because that way I know that it works, how it works, what
> its performance is like, etc.

I abhor code copying.
If I find that I reuse a function, I put it into a library.

>> I certainly have no intention of comparing strings with numbers &c.
>> My question was about a list of _homogeneous_ lists, and comparing to,
>> say, lists of numbers, is done lexicographically based on number
>> comparison.
>
> We could certainly add comparison functions for lists, then ones for
> arrays,

nah, all sequences are compared the same way.

> then for hash tables, and so on.
> Or just write one that works for all built-in types and impose an
> ordering between objects of distinct types. It would be more useful
> (if harder to write).

I seriously doubt this could be useful.

-- 
Sam Steingold (https://aphar.dreamwidth.org/) on darwin Ns 10.3.2113
https://lastingimpactpsychology.com https://steingoldpsychology.com
https://iris.org.il https://ij.org/ https://jij.org https://camera.org
Calculus has its limits.



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

* Re: lexicographic list comparison
  2022-09-12 18:31       ` Stefan Monnier
@ 2022-09-12 18:41         ` Sam Steingold
  2022-09-12 19:32           ` Stefan Monnier
  0 siblings, 1 reply; 10+ messages in thread
From: Sam Steingold @ 2022-09-12 18:41 UTC (permalink / raw)
  To: emacs-devel, Stefan Monnier

> * Stefan Monnier <zbaavre@veb.hzbagerny.pn> [2022-09-12 14:31:33 -0400]:
>
> [ Tho I suspect you'll want those comparison functions to return
>   a -1/0/1 result rather than just a boolean, so you'd need an extra
>   combinator to turn such a comparison function into the kind expected
>   by `sort`.  ]

The predicate argument to lisp sort returns a boolean.

-- 
Sam Steingold (https://aphar.dreamwidth.org/) on darwin Ns 10.3.2113
https://lastingimpactpsychology.com https://steingoldpsychology.com
https://jihadwatch.org https://www.dhimmitude.org https://iris.org.il
You won't get smarter by calling me a fool.



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

* Re: lexicographic list comparison
  2022-09-12 18:41         ` Sam Steingold
@ 2022-09-12 19:32           ` Stefan Monnier
  0 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2022-09-12 19:32 UTC (permalink / raw)
  To: emacs-devel

Sam Steingold [2022-09-12 14:41:47] wrote:
>> * Stefan Monnier <zbaavre@veb.hzbagerny.pn> [2022-09-12 14:31:33 -0400]:
>> [ Tho I suspect you'll want those comparison functions to return
>>   a -1/0/1 result rather than just a boolean, so you'd need an extra
>>   combinator to turn such a comparison function into the kind expected
>>   by `sort`.  ]
>
> The predicate argument to lisp sort returns a boolean.

Indeed, but `list-compare` will likely want the other kind, hence the
need for combinators that change from kind to the other.


        Stefan




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

end of thread, other threads:[~2022-09-12 19:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-09 19:27 lexicographic list comparison Sam Steingold
2022-09-10  9:07 ` Mattias Engdegård
2022-09-10  9:22   ` Emanuel Berg
2022-09-10  9:26   ` tomas
2022-09-12 14:45   ` Sam Steingold
2022-09-12 17:53     ` mattiase
2022-09-12 18:31       ` Stefan Monnier
2022-09-12 18:41         ` Sam Steingold
2022-09-12 19:32           ` Stefan Monnier
2022-09-12 18:35       ` Sam Steingold

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).