如何删除列表方案中的所有重复元素?

发布于 2024-11-02 08:33:55 字数 347 浏览 1 评论 0原文

我的尝试是,

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

  )

我的列表是(abcaaccc) 我想要的是(abc)。有什么想法吗?

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哭了丶谁疼 2024-11-09 08:33:55

我将通过循环使用由可见元素构建的第二个列表来解决此问题。如果这是家庭作业,我会为给你这个而感到难过 - 理解递归的工作原理比仅仅得到正确的答案更重要。

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

更新以适应您的评论 - 这可能不是最干净的解决方案,但应该让您了解它的工作原理。

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

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)))))))
静待花开 2024-11-09 08:33:55

R5RS+SRFI1

(define (remove-duplicates lst)
    (fold-right (lambda (f r)
             (cons f (filter (lambda (x) (not (equal? x f))) r))) '() lst))

R5RS + SRFI1

(define (remove-duplicates lst)
    (fold-right (lambda (f r)
             (cons f (filter (lambda (x) (not (equal? x f))) r))) '() lst))
望喜 2024-11-09 08:33:55

使用 SRFI 1,您可以直接使用 delete-duplicatesdelete-duplicates!http://srfi.schemers.org/srfi-1/srfi-1.html#delete-duplicates

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文