Is there a common LISP function to compare the contents of two lists?

送分小仙女□ 提交于 2019-12-08 19:23:25

问题


In particular, I just want to ensure that two lists have the same elements, ignoring order


回答1:


According to Steele "set-difference returns a list of elements of list1 that do not appear in list2. This operation is not destructive."

So if the set-difference is empty and the lengths are the same...

http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node152.html#SECTION001950000000000000000




回答2:


If order isn't important, you can use equal. For instance,

(equal (list 1 2) (list 1 2))

is true. Thus one way to do it would be to (sort) the list and then use equal. Note that sort is destructive so if order matters, you might want to copy it first.




回答3:


(defun same-bag-p (bag1 bag2 &key (test #'eql))
  (let ((table (make-hash-table :test test)))
    (loop for key in bag1 do (incf (gethash key table 0)))
    (loop for key in bag2 do (decf (gethash key table 0)))
    (loop for val being each hash-value of table always (= val 0))))



回答4:


If repeating items are not important see also SET-EXCLUSIVE-OR.




回答5:


If the order isn't important you can use "equal-set":

(equal-sets (1 2)(1 2)) -> T

(equal-sets (1 2)(2 1)) -> T

(equal-sets (1 2 5)(1 2)) -> NIL

(equal-sets (1 2)(1 5 2)) -> NIL




回答6:


Sort both lists, then compare:

(equal (sort l1 #'<) (sort l2 #'<))



回答7:


(defun list= (l1 l2 &key (test #'eql))
  (loop for i in l1
     for j in l2
     always (funcall test i j)))


(list= '(1 2 "a") '(1 2 "a") :test #'equal) ;; => T
(list= '(1 2 "a") '(1 2 "a") :test #'eql) ;; => NIL


来源:https://stackoverflow.com/questions/4110742/is-there-a-common-lisp-function-to-compare-the-contents-of-two-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!