Scheme-检查列表的结构等价性(如何使用和)

Scheme-检查列表的结构等价性(如何使用和),scheme,Scheme,我正试图编写一个程序来检查一些列表输入的结构等价性,无论它只包括原子还是嵌套的子列表 我在使用和方面有困难,我甚至不知道这是否可能,而且我似乎无法理解我正在查看的文档 我的代码: (define (structEqual a b) (cond (((null? car a) AND (null? car b)) (structEqual (cdr a) (cdr b))) (((null? car a) OR (null? car b)) #f) (((pa

我正试图编写一个程序来检查一些列表输入的结构等价性,无论它只包括原子还是嵌套的子列表

我在使用和方面有困难,我甚至不知道这是否可能,而且我似乎无法理解我正在查看的文档

我的代码:

 (define (structEqual a b)
   (cond
     (((null? car a) AND (null? car b)) (structEqual (cdr a) (cdr b)))
     (((null? car a) OR (null? car b)) #f)
     (((pair? car a) AND (pair? car b)) 
      (if (= (length car a) (length car b))
          (structEqual (cdr a) (cdr b))
          #f))
     (((pair? car a) OR (pair? car b)) #f)
     (else (structEqual (cdr a) (cdr b)))))
这个想法是(我想):(当我说两者时,我指的是a或b的当前cdr)

  • 检查a和b是否都为空,则它们在结构上相等

  • 检查是否只有a或b为空,则它们在结构上不相等

  • 检查它们是否都是成对的

  • 如果它们都是成对的,那么看看这对的长度是否相等,如果不是,它们在结构上是否相等

  • 如果它们不是两对,那么如果其中一对是一对,而另一对不是,那么它们在结构上是不等价的

  • 如果它们都不是成对的,那么它们都必须是原子,所以它们在结构上是等价的

  • 如你所见,我试图通过检查a或b的car的等价性来递归地实现这一点,然后如果它们失败,则返回#f,或者如果它们在每一步都等价,则继续到每个car的cdr


    有什么帮助吗?

    仅Scheme(或任何LISP)前缀中没有中缀运算符。每次接线员先来
    (或x(和y z q)(和y w e))
    ,其中每个字母都可以是一个复杂的表达式。所有不是
    #f
    的东西都是真值。因此
    (如果4'a'b)
    计算为
    a
    ,因为4是真值<代码>汽车需要括号

    当在
    cond
    中计算另一个谓词时,您应该利用以下事实:在此之前的一切都是假的。例如

    (define (structure-equal? a b)
      (cond
        ((null? a) (null? b))                                ; if a is null the result is if b is null
        ((not (pair? a)) (not (pair? b)))                    ; if a is not pair the result is if b is not also
        ((pair? b) (and (structure-equal? (car a) (car b))   ; if b is pair (both a and b is pair then) both 
                        (structure-equal? (cdr a) (cdr b)))) ; car and cdr needs to be structurally equal
        (else #f)))                                          ; one pair the other not makes it #f
    
    (structure-equal '(a (b (c d e) f) g . h) '(h (g (f e d) c) b . a)) ; ==> #t
    

    仅Scheme(或任何LISP)前缀中没有中缀运算符。每次接线员先来
    (或x(和y z q)(和y w e))
    ,其中每个字母都可以是一个复杂的表达式。所有不是
    #f
    的东西都是真值。因此
    (如果4'a'b)
    计算为
    a
    ,因为4是真值<代码>汽车需要括号

    当在
    cond
    中计算另一个谓词时,您应该利用以下事实:在此之前的一切都是假的。例如

    (define (structure-equal? a b)
      (cond
        ((null? a) (null? b))                                ; if a is null the result is if b is null
        ((not (pair? a)) (not (pair? b)))                    ; if a is not pair the result is if b is not also
        ((pair? b) (and (structure-equal? (car a) (car b))   ; if b is pair (both a and b is pair then) both 
                        (structure-equal? (cdr a) (cdr b)))) ; car and cdr needs to be structurally equal
        (else #f)))                                          ; one pair the other not makes it #f
    
    (structure-equal '(a (b (c d e) f) g . h) '(h (g (f e d) c) b . a)) ; ==> #t
    

    格式应该是(和expr1 expr2…)这是否也适用于或?我假设它们只返回一个布尔值?格式应该是(和expr1 expr2…),这是否也适用于或?我假设它们只返回一个布尔值?