I have a homework problem that is asking me to tell if two sets are equal in content, regardless of order.
Ex: (set-equal? (list 1 2 3) (list 3 2 1))
is true
I have gotten this code so far,
(define (set-equal? list1 list2)
(cond
[(and (empty? list1) (empty? list2)) true]
[(and (cons? list1) (empty? list2)) false]
[(and (empty? list1) (cons? list2)) false]
[(and (cons? list1) (cons? list2))
(if (member? (first list1) list2)
(set-equal? (rest list1) list2)
(set-equal? (rest list1) list2))]))
This code obviously doesn't work because even if two lists are equal, the recursion will lead to (empty) for list 1 and list 2 will still have data, making the final output false.
I think I should approach this way: Check the data in list1 against data in list2 and if there are equal data, remove it from both lists. Then keep checking until either both lists are empty (giving true) or one is empty and one still has data (outputting false). The problem is, I don't know how to code this.
Can anyone give me a little hint on how to fix this problem?