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)
有什么帮助吗?仅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…),这是否也适用于或?我假设它们只返回一个布尔值?