Search code examples
common-lispset-difference

Performing set-difference with Multiple Set Arguments


The function set-difference is restricted to finding the difference between two sets. Can this be efficiently extended to allow more set arguments--eg, (my-set-difference A B C)--in the same way the function - works--eg, (- 9 3 1) => 5? Using (reduce #'set-difference ...) is not very efficient, as it first requires appending all of the set arguments into a sequence.


Solution

  • Actually, I think concatenating all the lists except the first one is probably the best solution.

    Each invocation of set-difference will be O(n) (where n is the maximum size of the two lists), so reducing will be O(n*m) (where m is the number of lists). But if you do

    (set-difference A (append B C D E F ...))
    

    Appending all the lists is O(total length of B...), and the complexity of set-difference will be similar.