From: Sam Steingold <sds@gnu.org>
To: emacs-devel@gnu.org
Subject: lexicographic list comparison
Date: Fri, 09 Sep 2022 15:27:46 -0400 [thread overview]
Message-ID: <lz4jxg8i19.fsf@3c22fb11fdab.ant.amazon.com> (raw)
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.
next reply other threads:[~2022-09-09 19:27 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-09 19:27 Sam Steingold [this message]
2022-09-10 9:07 ` lexicographic list comparison 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=lz4jxg8i19.fsf@3c22fb11fdab.ant.amazon.com \
--to=sds@gnu.org \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).