equal.scm 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. ; Required structures:
  2. ; scheme-level-0
  3. ; byte-vectors
  4. ; (also requires not and eqv?, loaded from rts/base.scm into scheme-level-1)
  5. (define (equal?-recursor x y
  6. memo)
  7. (or (eqv? x y)
  8. (let ((memo-pair (assq x memo)))
  9. (and (pair? memo-pair)
  10. (eqv? (cdr memo-pair) y)))
  11. (cond
  12. ((pair? x)
  13. (and (pair? y)
  14. (let ((new-memo (cons (cons x y) memo)))
  15. (and (equal?-recursor (car x) (car y) new-memo)
  16. (equal?-recursor (cdr x) (cdr y) new-memo)))))
  17. ((vector? x)
  18. (and (vector? y)
  19. (let ((vlx (vector-length x)))
  20. (and (= vlx (vector-length y))
  21. (or (= vlx 0)
  22. (let ((new-memo (cons (cons x y) memo)))
  23. (do ((i 0 (+ i 1)))
  24. ((or (= i vlx)
  25. (not (equal?-recursor (vector-ref x i)
  26. (vector-ref y i)
  27. new-memo)))
  28. (= i vlx)))))))))
  29. ((string? x)
  30. (and (string? y)
  31. (string=? x y)))
  32. ((byte-vector? x)
  33. (and (byte-vector? y)
  34. (byte-vector=? x y)))
  35. (else #f))))
  36. (define (equal? x y)
  37. (equal?-recursor x y
  38. '()))