(defun samesetp (list1 list2)
(cond
((null list1) (null list2))
((eq list2 (remove (car list1) list2 :count 1)) nil)
(t (samesetP (cdr list1) (remove (car list1) list2 :count 1))))
)
)
首先,让我们正确格式化:
(defun samesetp (list1 list2)
(cond ((null list1) (null list2))
((eq list2 (remove (car list1) list2 :count 1)) nil)
(t (samesetP (cdr list1) (remove (car list1) list2 :count 1)))))
如果您使用的一种形式两次,要改变这种状况,那么你需要存储的值。 LET是这样的构造。如果它不适合一个COND,那么你需要第二个COND。
(defun samesetp (list1 list2)
(cond ((null list1) (null list2))
(t (let ((list3 (remove (car list1) list2 :count 1)))
(cond ((eq list2 list3) nil)
(t (samesetP (cdr list1) list3)))))))
现在,EQ不能用于比较列表。使用EQUAL。
(defun samesetp (list1 list2)
(cond ((null list1) (null list2))
(t (let ((list3 (remove (car list1) list2 :count 1)))
(cond ((equal list2 list3) nil)
(t (samesetP (cdr list1) list3)))))))
COND是矫枉过正这里,使用IF:
(defun samesetp (list1 list2)
(if (null list1)
(null list2)
(let ((list3 (remove (car list1) list2 :count 1)))
(if (equal list2 list3)
nil
(samesetP (cdr list1) list3)))))
现在,你只需要进行功能做什么被要求在作业。但无论如何,这是你的家庭作业。
你应该使用哈希表,这将会击垮距离O复杂性(N^2)到O(n)。 – leppie 2011-02-08 07:19:17
我很困惑。你正在使用一种可怕的算法,并且只要不涉及修复坏算法,就需要改进。为什么?正如@leppie所说的,使用哈希表。或者对两个列表进行排序,并行排列。更好的算法会比您所关心的微优化带来更多的不同。 – btilly 2011-02-08 07:30:58
让家伙冷静下来,我们不允许使用散列表。这是家庭作业问题的一部分。如果你能想到一个更有效的算法,可以包含在约10行代码中,请赐教。 – 2011-02-08 09:09:44