equal.scm 1.5 KB

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