A function to compare sets; help improving efficiency

前端 未结 2 1945
独厮守ぢ
独厮守ぢ 2021-01-26 23:07

I am attempting to write a function which compares two lists to see if they represent the same set. That is \'(a b c d d) and \'(d c b a d) represent

相关标签:
2条回答
  • (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))))
        )
    )
    

    First let's format it correctly:

    (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)))))
    

    If you use a form twice and you want to change that, then you need to store a value. LET is the construct for that. If it doesn't fit into one COND, then you need a second one.

    (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)))))))
    

    Now, EQ can't be used to compare lists. Use 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 is overkill here, use 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)))))
    

    Now, you only need to make the function do what was asked in the homework. But it is your homework anyway.

    0 讨论(0)
  • 2021-01-27 00:03

    I guess you are not allowed to use built-in functions for solving the problem, but just to note there is one:

    (set-difference list1 list2)
    
    0 讨论(0)
提交回复
热议问题