Sujet : Re: Rather simple list/set operation [?]
De : enometh (at) *nospam* meer.net (Madhu)
Groupes : comp.lang.lispDate : 08. Jun 2024, 18:05:34
Autres entêtes
Organisation : Motzarella
Message-ID : <m35xuj773l.fsf@leonis4.robolove.meer.net>
References : 1 2
* Jeff Barnett <
v3vi98$25sr1$1@dont-email.me> :
Wrote on Fri, 7 Jun 2024 12:07:33 -0600:
On 6/7/2024 1:56 AM, W J wrote:
From: Drew Krause
Subject: rather simple list/set operation
Date: Tue, 3 Jan 2012 19:01:19 -0600
Drew Krause wrote:
Maybe someone can help me with this?
I start with a list, e.g.
>
((0) (1 3) (1 2) (4 6) (5 7) (7 8))
>
>
and want all members of intersecting lists to appear in the same sublist:
>
=> ((0) (1 2 3) (4 6) (5 7 8))
I think the problem definition is ambiguous and not clarified by the
example.
Consider the following input lists:
((0 1) (1 2) (2 3)),
((0 1) (0 2) (0 3)).
Any idea what Krause had in mind?
One way to answer this is to write a program and define the problem to
by what the program solves.
Here is a suitably cringeworthy function to do that in lisp.
(defun krause-group (list-of-lists &aux ret)
(dolist (list list-of-lists)
(assert (consp list))
(let ((cons (or (find-if (lambda (x) (find (car list) x)) ret)
(let ((cons (list (car list))))
(push cons ret)
cons))))
(dolist (elt (cdr list))
(unless (find elt cons)
(setf (cdr cons) (cons elt (cdr cons)))))))
(nreverse ret))
(krause-group '((0) (1 3) (1 2) (4 6) (5 7) (7 8)))
=> ((0) (1 2 3) (4 6) (5 8 7))
(krause-group '((0 1) (1 2) (2 3)))
(krause-group '((0 1) (0 2) (0 3)))
So obviously krause wanted both of these lists to produce 1 list
((0 3 2 1))