2.54

练习 2.54 如果两个表包含着同样元素,这些元素也按同样顺序排列,那么就称这两个表 equal?。例如:

(equal? '(this is a list) '(this is a list))

是真;而

(equal? '(this is a list) '(this (is a) list))

是假。说得更准确些,我们可以从符号相等的基本eq?出发,以递归方式定义出 equal?。a和b是equal?的,如果它们都是符号,而且这两个符号满足eq?;或者它们都是表,而且(car a)和(car b)相互equal?,它们的(cdr a)和(cdr b)也是equal?。请利用这一思路定义出equal?过程。


(define (equal? a b)
    (if (and (pair? a) (pair? b))
        (and 
            (equal? (car a) (car b))
            (equal? (cdr a) (cdr b))
        )
        (eq? a b)
    )
)

(equal? '(this is a list) '(this is a list))
(equal? '(this is a list) '(this (is a) list))
(equal? 'a 'a)
(equal? 'a 'b)

results matching ""

    No results matching ""