• Subject: Re: Detele repeated in a list

    From B. Pym@21:1/5 to Mariano Montone on Sat Aug 9 10:23:53 2025
    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))


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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Sat Aug 9 18:43:57 2025
    XPost: comp.lang.scheme

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

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Sat Aug 9 18:35:36 2025
    XPost: comp.lang.scheme

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


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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Sat Aug 9 22:46:55 2025
    XPost: comp.lang.scheme

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Thu Aug 28 15:36:27 2025
    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))

    Pascal Costanza wrote:

    (defun rem-duplicates (list)
    (loop for (first . rest) on (append list list)
    unless (member first (reverse rest) :test #'equal)
    collect first))

    (defun rem-duplicates (seq)
    (reduce
    'adjoin
    seq
    :from-end t :initial-value '()))

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)