Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Abandon the search for Truth; settle for a good fantasy.


comp / comp.lang.lisp / P09

SubjectAuthor
* P09B. Pym
`- Re: P09HenHanna

1
Subject: P09
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 9 Jun 2024 13:32 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: P09
Date: Sun, 9 Jun 2024 13:32:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <v44atd$3he21$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 09 Jun 2024 15:32:33 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5670d48adda255666aa0101125ed0c51";
logging-data="3717185"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19xEApUfucpoCSCF+2iNCS9"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:2fltW/FJ9va+IK0o6PUjEDBKZf8=
View all headers

P09 (**) Pack consecutive duplicates of list elements into sublists.

If a list contains repeated elements they should be placed in
separate sublists.

Example:
* (pack '(a a a a b c c a a d e e e e))
((A A A A) (B) (C C) (A A) (D) (E E E E))

Pascal Bourguignon wrote:

> ;; Nice recursive solution:
>
> (defun group (list)
> (labels ((group-run (element group list)
> (cond
> ((null list) (list (cons element group)))
> ((eql element (first list)) (group-run element (cons element group) (rest list)))
> (t (cons (cons element group) (group-run (first list) '() (rest list)))))))
> (if (null list)
> '()
> (group-run (first list) '() (rest list)))))
>
>
>
> ;; Smartass solution, using Common Lisp reduce:
>
> (defun group (list)
> (reduce (lambda (item result)
> (cond
> ((endp result) (list (list item)))
> ((eql (first (first result)) item) (cons (cons item (first result))
> (rest result)))
> (t (cons (list item) result))))
> list
> :from-end t
> :initial-value '()))

Gauche Scheme

(use srfi-1) ;; span

(define (group lst)
(if (null? lst)
'()
(let-values (((a b) (span (cut equal? (car lst) <>) lst)))
(cons a (group b)))))

(group '(2 2 foo bar bar j j j k baz baz))
===>
((2 2) (foo) (bar bar) (j j j) (k) (baz baz))

Subject: Re: P09
From: HenHanna
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 9 Jun 2024 23:23 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@devnull.tb (HenHanna)
Newsgroups: comp.lang.lisp
Subject: Re: P09
Date: Sun, 9 Jun 2024 16:23:03 -0700
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <v45dhf$3s5s5$1@dont-email.me>
References: <v44atd$3he21$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 10 Jun 2024 01:23:28 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="fdf3da398add0912fb8385f840f76bc6";
logging-data="4069253"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Y0auOirpGoYhkNakZcSuLTiYHekINssM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:LfaI48+OnJv46fcbUfmmiPa8MR0=
Content-Language: en-US
In-Reply-To: <v44atd$3he21$1@dont-email.me>
View all headers

On 6/9/2024 6:32 AM, B. Pym wrote:
> P09 (**) Pack consecutive duplicates of list elements into sublists.
>
> If a list contains repeated elements they should be placed in
> separate sublists.
>
> Example:
> * (pack '(a a a a b c c a a d e e e e))
> ((A A A A) (B) (C C) (A A) (D) (E E E E))
>
> Pascal Bourguignon wrote:
>
>> ;; Nice recursive solution:
>>
>> (defun group (list)
>> (labels ((group-run (element group list)
>> (cond
>> ((null list) (list (cons element group)))
>> ((eql element (first list)) (group-run element (cons element group) (rest list)))
>> (t (cons (cons element group) (group-run (first list) '() (rest list)))))))
>> (if (null list)
>> '()
>> (group-run (first list) '() (rest list)))))
>>
>>
>>
>> ;; Smartass solution, using Common Lisp reduce:
>>
>> (defun group (list)
>> (reduce (lambda (item result)
>> (cond
>> ((endp result) (list (list item)))
>> ((eql (first (first result)) item) (cons (cons item (first result))
>> (rest result)))
>> (t (cons (list item) result))))
>> list
>> :from-end t
>> :initial-value '()))
>
> Gauche Scheme
>
> (use srfi-1) ;; span
>
> (define (group lst)
> (if (null? lst)
> '()
> (let-values (((a b) (span (cut equal? (car lst) <>) lst)))
> (cons a (group b)))))
>
> (group '(2 2 foo bar bar j j j k baz baz))
> ===>
> ((2 2) (foo) (bar bar) (j j j) (k) (baz baz))

(use srfi-1) ;; span

(define (gp x)
(if (null? x) '()
(let-values (((F L) (span (cut equal? (car x) <>) x)))
(cons F (gp L)))))

(print (gp '(a b b a a a b b b b)))
(print (gp '(c c c a d d d d a e e e e e)))

(define (gpC x) (map (lambda (x) (list (car x) (length x))) (gp x)))

(print (gpC '(a b b a a a b b b b)))
(print (gpC '(c c c a d d d d a e e e e e)))

There's no way to write (lambda (x) (list (car x) (length x)))
using Cut or Cute???

1

rocksolid light 0.9.8
clearnet tor