Exercise 2.54
Two lists are said to be
equal?
if they contain equal elements arranged in the same order.For example,
(equal? '(this is a list) '(this is a list))
is true, but
(equal? '(this is a list) '(this (is a) list))
is false. To be more precise, we can define
equal?
recursively in terms of the basiceq?
equality of symbols by saying thata
andb
areequal?
if they are both symbols and the symbols areeq?
, or if they are both lists such that(car a)
isequal?
to(car b)
and(cdr a)
isequal?
to(cdr b)
. Using this idea, implementequal?
as a procedure.
I wrote the following:
(define (equal? a b)
(or (and (and (symbol? a) (symbol? b)) (eq? a b))
(and (null? a) (null? b))
(and (and (and (not (null? a))
(list? a))
(and (not (null? b))
(list? b)))
(and (equal? (car a) (car b))
(equal? (cdr a) (cdr b))))))
Can this be improved?