问题
My attempt was,
(define (remove-dup lst)
(cond ((null? lst) '())
((null? (cdr lst)) (car lst))
((equal? (car lst) (car (cdr lst))) (remove-dup (cdr lst)))
(else (cons (car lst) (remove-dup (cdr lst))))
)
)
My list was (a b c a a c c c )
What I want is (a b c)
. Any idea?
Thanks,
回答1:
I'd approach this by looping with a second list that you build up of seen elements. I'll feel bad for giving this to you if this was homework though - it's more important to understand how recursion works than to just have the right answer.
(define (remove-dup ls)
(let loop ((ls ls) (seen '()))
(cond
((null? ls) '())
((memq (car ls) seen) (loop (cdr ls) seen))
(else (cons (car ls) (loop (cdr ls) (cons (car ls) seen))))))
Updated to accommodate your comments - this probably isn't the cleanest solution but should give you an idea of how it might work.
(define (rdup ls)
(let loop ((ls ls) (current #f)) ; this is bad coding style, a "magic" variable you don't expect to see in your list
(cond
((null? ls) '())
((null? (cdr ls)) (if (eq? (car ls) current) '() ls))
((eq? (car ls) (cadr ls)) (loop (cdr ls) (car ls)))
((eq? (car ls) current) (loop (cdr ls) current))
(else (cons (car ls) (loop (cdr ls) (car ls)))))))
回答2:
R5RS + SRFI1
(define (remove-duplicates lst)
(fold-right (lambda (f r)
(cons f (filter (lambda (x) (not (equal? x f))) r))) '() lst))
回答3:
Using SRFI 1 you can use directly delete-duplicates
or delete-duplicates!
: http://srfi.schemers.org/srfi-1/srfi-1.html#delete-duplicates
来源:https://stackoverflow.com/questions/5740147/how-to-erase-all-duplicate-elements-in-a-list-scheme