In particular, I just want to ensure that two lists have the same elements, ignoring order
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.
(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))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With