How to erase all duplicate elements in a list Scheme?

两盒软妹~` 提交于 2019-12-24 09:20:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!