我想知道list1是否包含在list2中;也称为子列表,但我在代码的结尾部分遇到了麻烦



;;问题是我想计算a列表是否包含在另一个列表中,而不管 ;;(list=? (list 1 2 3) (list 2 2 2 2 3 1 2 3 4))的大小应该产生true,但对于我的程序,它不是。

请解释/修复我出错的地方我的代码是:

(define (list=? a-list another-list)
   (cond
       [(empty? a-list) (empty? another-list)]
       [(cons? a-list)
              (and (cons? another-list)
              (and (= (first a-list) (first another-list))
                   (list=? (rest a-list) (rest another-list))))]))

这在Racket中非常简单,只需使用subset?过程:

(define (list=? a-list another-list)
  (subset? a-list another-list))

(list=? (list 1 2 3) (list 2 2 2 2 3 1 2 3 4))
=> #t

最新更新