2011-04-15 51 views
0

我遇到了我的方案计划问题。我试着拿两份名单并比较它们的大小,并且返回true表示大小相等,如果不是,则返回false。每个原子的价值并不重要。方案:比较列表尺寸

例子:

(structeq '(a (b(c))) '(1(2(3)))) => #t 
(structeq '(x) '(()) => #f 

这里是我的代码:

(define (structeq list1 list2) 
    (cond ((null? list1) list2) 
    (eq? (length list1) (length list2)))) 

(structeq '(a b c d) '(a b c)) 

然而,这种返回最后一个列表的大小。我哪里错了?

编辑:取消此问题。我想通了,我只需要删除cond语句。

回答

2

需要注意的是:

(define (same-length a b) 
    (if (and (null? a) (null? b)) #t 
     (if (or (null? a) (null? b)) #f 
      (same-length (cdr a) (cdr b))))) 

会,因为它发现了短名单的末尾,尽快停止。

0
(eq? (length list1) (length list2)))) 

代码中的这一行有一个谓词,但没有结果。如果他们是平等的,你想返回#t。
当列表长度不相等时,还可以添加其他情况来捕获。例如:(else #f)

查看更多about conditionals here