Reputation: 780
I am looking for a function that compares or checks if 2 lists are equal.
It should ignore number order and duplicated numbers...
I already created a function that checks if a element is part of a list on another block, but I am having some problems with this one now.
Here's what I have
(define (ispart? x b)
(cond ((null? b) #f)
((= x (car b)) #t)
(else (ispart? x (cdr b)))))
(define (check=? c1 c2)
(define (verification-1 c1 c2)
(cond((null? c1)
#t)
((ispart? (car c1) c2) (check=? (cdr c1) c2))))
(define (verification-2 c1 c2)
(cond((null? c2)
#t)
((ispart? (car c2) c1) (check=? c1 (cdr c2)))))
(if (equal? (and verification-1 verification-2) #t)
#t
(#f)))
I'm having problems with the conditions mainly, I am creating a check for each element of the first list to see if it belongs to the second list and then a check for each element of the second list. If any element fails that check then the lists are different.
I also tried this one but it doesn't ignore duplicated numbers or number order, so (3 2 1) is different from (1 2 2 3) and I don't want that to happen
(define (lists=? lst1 lst2)
(cond ((null? lst1) (null? lst2))
((null? lst2) #f)
((= (car lst1) (car lst2))
(lists=? (cdr lst1) (cdr lst2)))
(else #f)))
Upvotes: 1
Views: 5774
Reputation: 8448
Given a function all
, which says if all elements of a list are true,
(define (all l)
(cond ((null? l) #t)
((equal? #f (car l)) #f)
(else (all (cdr l)))))
and given your ispart?
function,
You can simply see if every element in the first list passes the ispart?
test in the second list, and vice-versa:
(define (set_equal l0 l1)
(and (all (map (lambda (x) (ispart? x l1))
l0))
(all (map (lambda (x) (ispart? x l0))
l1))))
Upvotes: 2