3

我对 lisp 相当陌生,并试图通过区分两个列表来解决这个问题。我需要找到列表 2 中所有不在列表 1 中的数字

期待得到(diff list1 list2)......例如(diff '(4 5 6 8 9) '(1 2 4 6 8 9))Return 将是(1 2)

到目前为止我所拥有的

(defun diff (l1 l2) 
  (cond ((null l1) nil) 
        ((member (first l1) l2) (diff(rest l1) l2)) 
        (t (cons (first l2) (diff (rest l2) l1)))))


(defun diff1 (l1 l2)
  (cond ((null l1) nil) 
        ((member (first l2) l1) (diff (rest l2) l1)) 
        (t (cons (first l2) (diff (rest l2) l1)))))

这是我尝试过的两种不同方法,但似乎无法让它返回列表 2 而不是列表 1。

4

1 回答 1

4

您的最后一行有错字diff

(defun diff (l1 l2)
  (cond ((null l2) nil) 
        ((member (first l2) l1) (diff l1 (rest l2))) 
        (t (cons (first l2) (diff l1 (rest l2))))))

请注意,一旦您正确格式化代码,该错误就会很明显:-)

此外,您要查找的函数已经存在于 Common Lisp 中,名称为set-difference

于 2013-10-23T00:23:34.180 回答