scheme word lists eq?

前端 未结 2 712
我寻月下人不归
我寻月下人不归 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:05

    Base on the comments from OP it's clear that these are set-eq?

    (set-eq? '(a b c) '(c b a)) ; ==> #t
    (set-eq? '(a b c) '(b c a)) ; ==> #t
    (set-eq? '() '())           ; ==> #t
    (set-eq? '(a b) '(a b c))   ; ==> #f
    (set-eq? '(a b c) '(a c))   ; ==> #f
    

    If the lists are without duplicates one could iterate the first list and try to find it in the second. If found we recurse with the two lists without the match.

    #!r6rs
    (import (rnrs)
            (rnrs lists))
    
    (define (set-eq? xs ys)
      (if (null? xs) 
          (null? ys) ; #t if both sets are empty, otherwise #f
          (let ((cur (car xs)))        
            (and (memq cur ys) ; first element is found 
                 (set-eq?  (remq  )))))) ; recurse without first in both lists
    

    There are ways to get this faster. E.q. Hash the first list and iterate the second. If all matches and hashtable-size is the same as the number of iterations it's #t.

提交回复
热议问题