开发者

Scheme: Compare List Sizes

I am having an issue with my Scheme program. I am trying to take in 2 lists and compare their sizes and return true is the sizes are equal, and false if they are not. The value of each atom doesn't matter.

Example:

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

Here is my code:

(defin开发者_如何学Ce (structeq list1 list2)
    (cond ((null? list1) list2)
    (eq? (length list1) (length list2))))

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

However, this returns the size of the last list. Where am I going wrong?

EDIT: Cancel this question. I figured it out, i just needed to remove the cond statement.


note that:

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

would stop as soon as it found the end of the shorter list.


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

This line in your code has a predicate but no consequent. If they are equal, you want to return #t.
Also would be good to add an else case to catch when the lists' length are not equal. eg: (else #f)

Read more about conditionals here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜