scheme word lists eq?

前端 未结 2 711
我寻月下人不归
我寻月下人不归 2021-01-28 20:14

i\'ve got a problem: I need to find if list equal to the second one, for example:

(set%eq? \'(1 2 3) \'(1 2 3))     ===> #t

(set%eq? \'(1 2 3) \'(2 3 4))             


        
2条回答
  •  情歌与酒
    2021-01-28 21:06

    There are a couple of mistakes in the posted code, and FYI, the procedure tests whether two lists are equal, it's not really testing for equality between two sets:

    (define (set-eq? xs ys)
      (cond ((and (null? xs) (null? ys)) #t)
            ((or (null? xs) (null? ys))  #f) ; missing case
            ((equal? (car xs) (car ys)) (set-eq? (cdr xs) (cdr ys))) ; use equal?
            ; deleted unnecessary case here. Anyway, why are you reversing the list?
            (else #f)))
    

    Now this will work:

    (set-eq? '(1 2 3) '(1 2 3))
    => #t
    
    (set-eq? '(1 2 3) '(2 3 4))
    => #f
    
    (set-eq? (quote ((quote one) (quote two) (quote three)))
             (quote ((quote one) (quote two) (quote three))))
    => #t
    

    In fact, this will work, too:

    (equal? '(1 2 3) '(1 2 3))
    => #t
    
    (equal? '(1 2 3) '(2 3 4))
    => #f
    
    (equal? (quote ((quote one) (quote two) (quote three)))
            (quote ((quote one) (quote two) (quote three))))
    => #t
    

    ...But this won't work, the lists are clearly different:

    (set-eq? '(1 2 3 4) '(4 1 2 3)) 
    => #f
    

    If you intended to test for equality between two sets, you have to completely rethink the algorithm. Here's an idea: write asubset? procedure that tests if a list is a subset of another list (that is: if all the elements in one list are contained in the other list), and then test whether (and (subset? l1 l2) (subset? l2 l1)) is true, if that happens, then they're equal according to the set definition of equality.

提交回复
热议问题