scheme - 我的 sicp 练习 2.54 的解决方案正确吗?

标签 scheme sicp

我发现这个练习很有趣。这是我的解决方案:

(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/

相关文章:

command-line - 如何在 Racket 中返回命令行结果?

functional-programming - (调用/抄送): What exactly is continuation?

python - SICP 练习 3.20 - 理解环境图(我的图中缺少绑定(bind))

scheme - 使用 trace 显示 Racket 中的过程

scheme - 为 SICP 解释 SCHEME

compiler-construction - SICP第5章中词法寻址的优点是什么?

scheme - if 和 cond 的区别?

scheme - 将 r5rs 文件包含到 Racket 中的另一个文件中

functional-programming - 在这种情况下如何在 Racket 上订购我的累积变量?

clojure - 如何美化Lisp源代码?