我正在阅读“Little Schemer”一书,并执行各种功能。一般来说,我最终会得到与书籍相同的版本,但不是 eqlist?,这是一个测试两个列表是否相等的函数。
我试过测试我的版本,它通过了我扔给它的任何东西。然而,它与“Little Schemer”版本略有不同,我希望有人对我是否遗漏一些东西发表意见——我怀疑是这样。
我的版本:
(define eqlist?
(lambda (list1 list2)
(cond
((and (null? list1)(null? list2))#t)
((or (null? list1)(null? list2))#f)
((and (atom? list1)(atom? list2))(eqan? list1 list2))
((or (atom? list1)(atom? list2)) #f)
(else
(and(eqlist? (car list1) (car list2))
(eqlist? (cdr list1) (cdr list2)))))))
书的版本:
(define eqlist2? ;This is Little Schemer's version
(lambda (list1 list2)
(cond
((and (null? list1)(null? list2)) #t)
((or (null? list1)(null? list2)) #f)
((and (atom? (car list1))(atom? (car list2)))
(and (eqan? (car list1)(car list2))(eqlist2? (cdr list1)(cdr list2))))
((or (atom? (car list1))(atom? (car list2))) #f)
(else
(and (eqlist2? (car list1)(car list2))
(eqlist2? (cdr list1)(cdr list2)))))))
在这两种情况下,eqan 的定义是:
(define eqan?
(lambda (a1 a2)
(cond
((and (number? a1)(number? a2)) (equal? a1 a2))
((or (number? a1)(number? a2)) #f)
(else (eq? a1 a2)))))
谢谢!
乔斯·德拉格