Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 52:40:52 |
Calls: | 583 |
Files: | 1,139 |
D/L today: |
179 files (27,921K bytes) |
Messages: | 111,617 |
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)))))))
(6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4): (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2)
(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))
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)))))))
(6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4): (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2)
(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))
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)))))))
(6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4): (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2)
(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 ...) ]))
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)))))))
(6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4): (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2)
(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)))
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)))))))
(6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4): (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2)
(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))
(defun rem-duplicates (list)
(loop for (first . rest) on (append list list)
unless (member first (reverse rest) :test #'equal)
collect first))