我发现这个练习很有趣。这是我的解决方案:
(define (my-equal? a b)
(cond ((eq? a b) #t)
((and (pair? a) (pair? b))
(and (my-equal? (car a) (car b)) (my-equal? (cdr a) (cdr b))))
(else #f)))
是吗?我想知道 (eq? a b) 是否为真,(equal? a b) 应该始终为真。
最佳答案
我认为我们可以通过考虑其他数据类型并递归测试正确/不正确列表中的元素来给出更准确的答案。这是我的镜头:
(define (same-type? a b)
(or (and (number? a) (number? b))
(and (symbol? a) (symbol? b))
(and (string? a) (string? b))
(and (list? a) (list? b))
(and (pair? a) (pair? b))))
(define (my-equal? a b)
(cond ((not (same-type? a b)) #f)
((or (symbol? a) (null? a) (null? b))
(eq? a b))
((list? a)
(if (not (= (length a) (length b)))
#f
(and (my-equal? (car a) (car b))
(my-equal? (cdr a) (cdr b)))))
((pair? a)
(and (my-equal? (car a) (car b))
(my-equal? (cdr a) (cdr b))))
((string? a) (string=? a b))
((number? a) (= a b))))
对于你问题的最后一部分,我建议你看一下这个非常详细的answer .
关于scheme - 我的 sicp 练习 2.54 的解决方案正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31040520/