Groups | Search | Server Info | Login | Register


Groups > comp.lang.scheme > #6545

Re: Subject: Re: Detele repeated in a list

From "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups comp.lang.lisp, comp.lang.scheme
Subject Re: Subject: Re: Detele repeated in a list
Date 2025-08-09 22:46 +0000
Organization A noiseless patient Spider
Message-ID <1078j4h$1ii7s$1@dont-email.me> (permalink)
References <10777jo$187fc$1@dont-email.me> <10784da$1f5th$1@dont-email.me> <10784sv$1faa7$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


B. Pym wrote:

> B. Pym wrote:
> 
> > B. Pym wrote:
> > 
> > > Mariano Montone wrote:
> > > 
> > > > > I have this piece of code, that sould delete all the repeated elements
> > > > > from a list :
> > > > > 
> > > > > (defun apaga-repetidos (lista)
> > > > >  (let ((p (first lista))
> > > > >     (r (rest lista)))
> > > > >   (if (null lista)
> > > > >     nil
> > > > >   (if (equal p r)
> > > > >     (apaga-repetidos r)
> > > > >     (cons p (apaga-repetidos r))))))
> > > > > 
> > > > 
> > > > You are comparing an element with a list when you do (equal p r)
> > > > 
> > > > Try this:
> > > > 
> > > > (defun apaga-repetidos (lista)
> > > >    (apaga-repetidos-2 lista (make-hash-table :test #'equal)))
> > > > 
> > > > (defun apaga-repetidos-2 (lista vistos)
> > > >    (if (null lista)
> > > >        nil
> > > >        (let ((p (first lista))
> > > >              (r (rest lista)))
> > > >          (if (gethash p vistos)
> > > >              (apaga-repetidos-2 r vistos)
> > > >              (progn
> > > >                (setf (gethash p vistos) t)
> > > >                (cons p (apaga-repetidos-2 r vistos)))))))
> > > > 
> > > > > > : (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2)
> > > >   (6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4)
> > > >   (4 5) (3 5) (3 6) (4 6) (4 5) (4 4) (5 4) ))
> > > > 
> > > > Result: ((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (7 2) (8 2) (8 3) (8 4)
> > > > (7 4) (6 4) (5 4)
> > > >  (4 4) (4 5) (3 5) (3 6) (4 6))
> > > 
> > > 
> > > (define (rem-dups List)
> > >   (do ((res '() (if (member x res) res (cons x res)))
> > >        (x #f))
> > >     ((null? List)  res)
> > >     (set! x (pop! List))))
> > > 
> > > 
> > > (rem-dups '((1 2) (2 2) (3 2) (4 2) (5 2)
> > >   (6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4)
> > >   (4 5) (3 5) (3 6) (4 6) (4 5) (4 4) (5 4)))
> > >   ===>
> > > ((4 6) (3 6) (3 5) (4 5) (4 4) (5 4) (6 4) (7 4) (8 4) (8 3) (8 2)
> > >  (7 2) (6 2) (5 2) (4 2) (3 2) (2 2) (1 2))
> > 
> > Shorter:
> > 
> > (define (rem-dups List)
> >   (Do ((r '() (if (member x r) r (cons x r)))
> >        (x (pop List) <>))
> >     ((null? List) @ r)))
> > 
> > (rem-dups '((1 2) (2 2) (3 2) (4 2) (5 2)
> >   (6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4)
> >   (4 5) (3 5) (3 6) (4 6) (4 5) (4 4) (5 4)))
> >   ===>
> > ((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (7 2) (8 2) (8 3) (8 4)
> >  (7 4) (6 4) (5 4) (4 4) (4 5) (3 5) (3 6) (4 6))
> > 
> > Given:
> > 
> > (define-syntax pop
> >   (syntax-rules ()
> >     [ (_ xs)
> >       (if (pair? xs)
> >         (begin0 (car xs) (set! xs (cdr xs)))
> >         #f) ] ))
> > (define-syntax Do-aux
> >   (syntax-rules (<> @)
> >     [(_ ((a b <>) d ...) (seen ...) z ...)
> >      (Do-aux (d ...) (seen ... (a b b)) z ...) ]
> >     [(_ ((a b c) d ...) (seen ...) z ...)
> >      (Do-aux (d ...) (seen ... (a b c)) z ...) ]
> >     [(_ ((a b) d ...) (seen ...) z ...)
> >      (Do-aux (d ...) (seen ... (a b)) z ...) ]
> >     [(_ ((a) d ...) (seen ...) z ...)
> >      (Do-aux (d ...) (seen ... (a '())) z ...) ]
> >     [(_ (a d ...) (seen ...) z ...)
> >      (Do-aux (d ...) (seen ... (a '())) z ...) ]
> >     [(_ () seen (a b ... @ xs) z ...)
> >      (Do-aux () seen (a b ... (reverse xs)) z ...) ]
> >     [(_ () seen till body ...)
> >      (do seen till body ...) ]))
> > (define-syntax Do
> >   (syntax-rules ()
> >     [(_ specs till body ...)
> >      (Do-aux specs () till body ...) ]))
> 
> Shorter yet:
> 
> (define (rem-dups List)
>   (Do ((r '() (if (member x r) r (cons x r)))
>        (x (pop List) <>))
>     ((not x) @ r)))

Shorter yet:

(define (rem-dups List)
  (Do ((r '() (adjoin x r))
       (x (pop List) <>))
    ((not x) @ r)))

Given:

(define (adjoin x xs) (if (member x xs) xs (cons x xs)))


-- 
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham

Back to comp.lang.scheme | Previous | NextPrevious in thread | Find similar


Thread

Re: Subject: Re: Detele repeated in a list "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-09 18:35 +0000
  Re: Subject: Re: Detele repeated in a list "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-09 18:43 +0000
    Re: Subject: Re: Detele repeated in a list "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-09 22:46 +0000

csiph-web