Is there a common LISP function to compare the con

2019-06-17 01:54发布

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

7条回答
爷的心禁止访问
2楼-- · 2019-06-17 02:19

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-17 02:19

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

查看更多
在下西门庆
4楼-- · 2019-06-17 02:28

Sort both lists, then compare:

(equal (sort l1 #'<) (sort l2 #'<))
查看更多
Bombasti
5楼-- · 2019-06-17 02:29

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楼-- · 2019-06-17 02:37
(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))))
查看更多
小情绪 Triste *
7楼-- · 2019-06-17 02:39
(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
查看更多
登录 后发表回答