• re: rather simple list/set operation

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Thu Aug 7 22:31:19 2025
    From Newsgroup: comp.lang.lisp

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

    Gauche Scheme

    (use srfi-1) ;; lset- functions

    (define (coalesce lists)
    (define result '())
    (dolist (xs lists)
    (let ((tmp0 '()) (tmp1 (list xs)))
    (dolist (ys result)
    (if (pair? (lset-intersection eqv? ys xs))
    (push! tmp1 ys)
    (push! tmp0 ys)))
    (set! result
    (cons (apply lset-union eqv? tmp1) tmp0))))
    result)


    (coalesce '((0) (1 3) (1 2) (4 6) (5 7) (7 8)))
    ===>
    ((8 5 7) (4 6) (0) (2 1 3))

    (coalesce '((2 4) (8 9) (4 5 8)))
    ===>
    ((5 9 8 2 4))

    (coalesce '((3 4) (7 8) (4 8) (0) (5 9)))
    ===>
    ((5 9) (8 7 3 4) (0))

    (coalesce '((3 4) (7 8) (4 5 8) (0) (5 9)))
    ===>
    ((9 5 8 7 3 4) (0))
    --
    [T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
    --- Kenny Tilton
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2