Functional programming 具有多个测试的Scheme语言案例条件?

Functional programming 具有多个测试的Scheme语言案例条件?,functional-programming,scheme,Functional Programming,Scheme,下面的伪代码显示了我要做的事情: case (test0, test1) begin (false,false): statement 0; (false,true): statement 1; (true,false): statement 2; (true,true): statement 3; end 如何使用case-conditional在scheme中实现这一点?问题是该案例使用了eqv?谓词,它似乎总是返回false(因为(eqv?'(#f

下面的伪代码显示了我要做的事情:

case (test0, test1)
  begin
    (false,false): statement 0;
    (false,true): statement 1;
    (true,false): statement 2;
    (true,true): statement 3;
  end
如何使用case-conditional在scheme中实现这一点?问题是该案例使用了
eqv?
谓词,它似乎总是返回false(因为
(eqv?'(#f.#f)(#f.#f))
的计算结果为
#f
)。如果能以任何合理简洁的方式在scheme中表达上述模式,我将不胜感激(除了明显地将其分解为嵌套的if条件)


编辑:下面的有效答案让我稍微重新构造我的问题:一个经验丰富的策划师如何编写上述模式?

一种方法,说明了一种方法:

(case (+ (if test0 10 0) (if test1 1 0))
  ((11) …)
  ((10) …)
  ((01) …)
  ((00) …))
如果
test0
test1
之间的重要性存在任何不对称,我会首先使用
If
和最重要的
测试。因此,假设
test0
更重要:

(if test0
    (if test1
        …  ;; (and test0 test1)
        …) ;; (and test0 (not test1))
    (if test1
        …   ;; …
        …)) ;; … 
如果重要性没有任何差异,则:

(cond ((and test0 test1) …)
      ((and test0 (not test1)) …)
      …)
如果它是一个带有两个变量的公共模式,那么我将定义一个宏,允许我在test0和test1的词法绑定中指定四个主体:

(define-syntax if-two-way
  (syntax-rules (tt tf ft ff)
    ((if-two-way test0 test1
       (tt btt1 btt …)
       (tf btf1 btf …)
       (ft bft1 bft …)
       (ff bgg1 bff …))
     (let ((t0 test0) (t1 test1))
       (if t0
           (if t1
               (begin btt1 btt …)
               (begin btf1 btf …))
           …))))))

这似乎是一个很好的适合球拍的:

例如:

(test '(#t . #f))
=> "statement 3"

(test (cons (= 0 0) (= 1 1)))
=> "statement 4"

(test '(a . b))
=>  unknown expression: (a . b)

使用
cond
equal?
更容易:

(define (test pair)
  (cond
    ((equal? pair '(#f . #f)) "statement 0")
    ((equal? pair '(#f . #t)) "statement 1")
    ((equal? pair '(#t . #f)) "statement 2")    
    ((equal? pair '(#t . #t)) "statement 3")
    (else                     "wot?")))

看起来您需要在Scheme中进行一些模式匹配。我相信你可以找到这样的宏包。Basile,我可以用Haskell以一种几乎类似于所示伪代码的方式表达所述模式。由于这似乎是一种相当常见的模式,希望它可以在scheme中简单地完成,而不必求助于宏……这似乎是一种相当好的方法,因为类似的匹配实现通常也可以在其他scheme实现中找到。
(define (test pair)
  (cond
    ((equal? pair '(#f . #f)) "statement 0")
    ((equal? pair '(#f . #t)) "statement 1")
    ((equal? pair '(#t . #f)) "statement 2")    
    ((equal? pair '(#t . #t)) "statement 3")
    (else                     "wot?")))