1

我了解当有列表和变量时如何删除元素,但是有没有办法使用另一个列表从列表中删除元素?示例: (list 1 2 3 4 5)(list 1 2 3) 产生 (list 4 5)

4

2 回答 2

1

在 Racket 中,这非常简单,只需使用remove*内置程序:

(remove* (list 1 2 3) (list 1 2 3 4 5))
=> '(4 5)
于 2013-03-20T14:06:04.973 回答
1

和变量一样,但是需要使用成员函数而不是equal?:

#lang racket
; remove every element included in rlist from flist
(define (remove-list rlist flist)
  (if (empty? flist)
      '()
      (let ((c (car flist)))
        (if (member c rlist) ; <====
            (remove-list rlist (cdr flist))
            (cons c (remove-list rlist (cdr flist)))))))

(remove-list (list 1 2 3) (list 1 2 3 4 5))
=> '(4 5)
于 2013-03-20T19:16:04.733 回答