我正在閱讀「Little Schemer」一書,並執行各種功能。一般來說,我最終會得到與本書相同的版本,但不是eqlist?,它是測試兩個列表相等的函數。小Schemer eqlist?功能 - 備用版本?
我試過測試我的版本,它傳遞了我扔到它的任何東西。然而,它與「小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)))))
謝謝!
喬斯德拉赫
+1從該小策士:-) – csl 2010-03-08 18:40:03