Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #62: need to wrap system in aluminum foil to fix problem


comp / comp.lang.lisp / Re: Collecting like-labelled sublists of a list

SubjectAuthor
* Re: Collecting like-labelled sublists of a listB. Pym
`- Re: Collecting like-labelled sublists of a listB. Pym

1
Subject: Re: Collecting like-labelled sublists of a list
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 22 Jul 2024 00:37 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Collecting like-labelled sublists of a list
Date: Mon, 22 Jul 2024 00:37:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <v7k9k9$ala1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 22 Jul 2024 02:37:31 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="57632485c335ebce11c84c4b6618c564";
logging-data="349505"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gb+A/ua2tHi7As7C3pI62"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:iLgjt1z4bFdw5aVcj7Bh76LDk7Q=
View all headers

Madhu wrote:

> |> (defun test (list)
> |> (loop for j in list
> |> for index = (first j)
> |> for k = (rest j)
> |> with indices = nil
> |> if (not (member index indices))
> |> do (pushnew index indices)
> |> and collect j into res
> |> else
> |> do (nconc (assoc index res) k) ; ASSOC instead of NTH
> |> finally (return res)))
> |
> | To be more precise (if that helps), I'm wondering if there's a way of
> | doing this without having to build up a list of the indices (labels)
> | and using membership/non-membership of this list as the test for
> | whether we have encountered a new index or not.
>
> You can get by without building indices and just using ASSOC (which you
> cannot avoid):
>
> (defun cortez-group (list) ; Destroys LIST!
> (let (result)
> (dolist (el list)
> (let ((entry (assoc (car el) result)))
> (if entry
> (rplacd entry (nconc (cdr entry) (cdr el)))
> (push el result))))
> (nreverse (mapcar #'cdr result))))
>
> * (setq $a '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)
> (2 k l) (4 m n) (2 o p) (4 q r) (5 s t)))
> * (cortez-group $a)
> => ((A B) (C D I J) (E F K L O P) (G H) (M N Q R) (S T))

Gauche Scheme

(use srfi-235) ;; group-by

(define (c-group lst)
(map
(cut append-map cdr <>)
((group-by car) lst)))

(c-group '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)
(2 k l) (4 m n) (2 o p) (4 q r) (5 s t)))

===>
((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))

Subject: Re: Collecting like-labelled sublists of a list
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Wed, 14 Aug 2024 23:57 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Collecting like-labelled sublists of a list
Date: Wed, 14 Aug 2024 23:57:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <v9jg9c$l9uo$1@dont-email.me>
References: <v7k9k9$ala1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Thu, 15 Aug 2024 01:57:34 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="8dd797abfa9c4845a90c0b3910c2697b";
logging-data="698328"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18cySYNZmugZV4ETo6ELUBA"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:6RYnzTeefqbLtZUxLVXYAPA9JYk=
View all headers

B. Pym wrote:

> Madhu wrote:
>
> > |> (defun test (list)
> > |> (loop for j in list
> > |> for index = (first j)
> > |> for k = (rest j)
> > |> with indices = nil
> > |> if (not (member index indices))
> > |> do (pushnew index indices)
> > |> and collect j into res
> > |> else
> > |> do (nconc (assoc index res) k) ; ASSOC instead of NTH
> > |> finally (return res)))
> > >
> > > To be more precise (if that helps), I'm wondering if there's a way of
> > > doing this without having to build up a list of the indices (labels)
> > > and using membership/non-membership of this list as the test for
> > > whether we have encountered a new index or not.
> >
> > You can get by without building indices and just using ASSOC (which you
> > cannot avoid):
> >
> > (defun cortez-group (list) ; Destroys LIST!
> > (let (result)
> > (dolist (el list)
> > (let ((entry (assoc (car el) result)))
> > (if entry
> > (rplacd entry (nconc (cdr entry) (cdr el)))
> > (push el result))))
> > (nreverse (mapcar #'cdr result))))
> >
> > * (setq $a '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)
> > (2 k l) (4 m n) (2 o p) (4 q r) (5 s t)))
> > * (cortez-group $a)
> > => ((A B) (C D I J) (E F K L O P) (G H) (M N Q R) (S T))
>
> Gauche Scheme
>
> (use srfi-235) ;; group-by
>
> (define (c-group lst)
> (map
> (cut append-map cdr <>)
> ((group-by car) lst)))
>
> (c-group '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)
> (2 k l) (4 m n) (2 o p) (4 q r) (5 s t)))
>
> ===>
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))

newLISP

(define (c-group lst)
(let (alist (map list (unique (map first lst))))
(dolist (xs lst)
(setf (assoc (xs 0) alist)
(append $it (1 xs))))
(map rest alist)))

(c-group '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j)
(2 k l) (4 m n) (2 o p) (4 q r) (5 s t)))

--->
((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))

1

rocksolid light 0.9.8
clearnet tor