List Struture Equality

Discussion of Scheme and Racket
saulgoode
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am

Re: List Struture Equality

Post by saulgoode » Thu Mar 22, 2012 5:17 pm

The following assumes that for (sub)lists to be considered similar to each other, they must have the same number of elements.

Code: Select all

(define (similar-struct? obj1 obj2)
  (cond
    ((and (atom? obj1) (atom? obj2))
      #t )
    ((atom? obj1)
      #f )
    ((atom? obj2)
      #f)
    (else
      (and (similar-struct? (car obj1) (car obj2))
           (similar-struct? (cdr obj1) (cdr obj2)) ))))

Post Reply