How to erase all duplicate elements in a list Sche

2019-08-06 12:15发布

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,

标签: scheme
3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-06 12:58

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)))))))
查看更多
Ridiculous、
3楼-- · 2019-08-06 13:04

R5RS + SRFI1

(define (remove-duplicates lst)
    (fold-right (lambda (f r)
             (cons f (filter (lambda (x) (not (equal? x f))) r))) '() lst))
查看更多
走好不送
4楼-- · 2019-08-06 13:16

Using SRFI 1 you can use directly delete-duplicates or delete-duplicates!: http://srfi.schemers.org/srfi-1/srfi-1.html#delete-duplicates

查看更多
登录 后发表回答