List 方案:比较列表大小
我的计划有问题。我试图在两个列表中比较它们的大小,如果大小相等,则返回true,如果大小不相等,则返回false。每个原子的值并不重要 例如:List 方案:比较列表大小,list,struct,scheme,equals,List,Struct,Scheme,Equals,我的计划有问题。我试图在两个列表中比较它们的大小,如果大小相等,则返回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 l
(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语句。注意:
(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)))))
一旦发现短名单的末尾,它就会停止
(eq? (length list1) (length list2))))
代码中的这一行有谓词,但没有结果。如果它们相等,则您希望返回#t.当列表的长度不相等时,最好添加一个else案例。例:(else#f) 阅读更多关于条件句的内容