Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #351: PEBKAC (Problem Exists Between Keyboard And Chair)


comp / comp.lang.lisp / P08 (**) Eliminate consecutive duplicates of list elements.

SubjectAuthor
* P08 (**) Eliminate consecutive duplicates of list elements.B. Pym
`- Re: P08 (**) Eliminate consecutive duplicates of list elements.B. Pym

1
Subject: P08 (**) Eliminate consecutive duplicates of list elements.
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 19 Aug 2024 08:14 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: P08 (**) Eliminate consecutive duplicates of list elements.
Date: Mon, 19 Aug 2024 08:14:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <v9uuse$2q1et$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 19 Aug 2024 10:14:09 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="9810b1c7a97d7b87e5fd6bb09612219a";
logging-data="2950621"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+s3IzPxTlac0B45dGU7f6V"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:Frx2AbsmlDbNeQ7dLYKj5qBeRDE=
View all headers

> If a list contains repeated elements they should be replaced
> with a single copy of the element. The order of the elements
> should not be changed.
>
> Example:
> * (compress '(a a a a b c c a a d e e e e))
> (A B C A D E)

In newLisp, "apply" can be used for reduce or fold.

(define (compress lst)
(reverse
(apply
(fn (accum x)
(cond ((empty? accum) (list x))
((= x (first accum)) accum)
(true (cons x accum))))
(cons '() lst)
2) ;; How many things to process at a time.
))

(compress '(a a a a b c c a a d e e e e))
===>
(a b c a d e)

Subject: Re: P08 (**) Eliminate consecutive duplicates of list elements.
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 19 Aug 2024 09:09 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: P08 (**) Eliminate consecutive duplicates of list elements.
Date: Mon, 19 Aug 2024 09:09:57 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <v9v251$2qfva$1@dont-email.me>
References: <v9uuse$2q1et$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 19 Aug 2024 11:09:58 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="9810b1c7a97d7b87e5fd6bb09612219a";
logging-data="2965482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19dsfJ6al0+aIvLNQm5zE7m"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:U41hzFOvnhVMLxSVT+WUy4LOm94=
View all headers

B. Pym wrote:

> > If a list contains repeated elements they should be replaced
> > with a single copy of the element. The order of the elements
> > should not be changed.
> >
> > Example:
> > * (compress '(a a a a b c c a a d e e e e))
> > (A B C A D E)
>
> In newLisp, "apply" can be used for reduce or fold.
>
> (define (compress lst)
> (reverse
> (apply
> (fn (accum x)
> (cond ((empty? accum) (list x))
> ((= x (first accum)) accum)
> (true (cons x accum))))
> (cons '() lst)
> 2) ;; How many things to process at a time.
> ))
>
> (compress '(a a a a b c c a a d e e e e))
> ===>
> (a b c a d e)

The list could first be converted to monotonic sublists.

(define (monotonic-slices lst key-func (cmp =))
(let (result '() tmp '() old-key 0 new-key 0)
(dolist (x lst)
(set 'new-key (key-func x))
(cond ((empty? tmp) (push x tmp))
((cmp new-key old-key) (push x tmp))
(true (push (reverse tmp) result) (set 'tmp (list x))))
(set 'old-key new-key))
(unless (empty? tmp) (push (reverse tmp) result))
(reverse result)))

(monotonic-slices '(0 2 3 4 5 7) odd?)
===>
((0 2) (3) (4) (5 7))

(monotonic-slices '(0 2 3 3 4 8 7 9) or >)
===>
((0 2 3) (3 4 8) (7 9))

So the solution to this problem is:

(map first (monotonic-slices '(a a a a b c c a a d e e e e) or))
===>
(a b c a d e)

1

rocksolid light 0.9.8
clearnet tor