在嵌套cond中的一行中混淆两个谓词

时间:2014-09-20 19:35:56

标签: scheme racket conditional-statements

我在理解在没有"和#34;的情况下在一行中使用两个谓词时遇到一些麻烦。或者"或"在球拍中。这是一个例子,其中有一条我很困惑的评论:

(define (question x) 
 (cond
  [(cond
     [(even? x) (< 20 x)]  ; what is this doing? Are they both being evaluated at once?
     [else (even? x)])     
   (cond
     [(odd? x) ’day]
     [else ’night])]
   [else ’goodbye]))

我不确定如果他们两个都评价为真,或者一个为真,另一个为假等,将会发生什么。如果有人能够对这个愚蠢的问题有所了解,请做,我会非常感谢!

2 个答案:

答案 0 :(得分:1)

cond条款具有以下形式:

(<test> <result> ...)

在这种情况下,<test>(cond [(even? x) (< 20 x)] [else (even? x)])<result>(cond [(odd? x) 'day] [else 'night])

如果我正确阅读,整体代码与

相同
(define (question x)
  (cond ((and (even? x) (< 20 x)) 'night)
        (else 'goodbye)))

答案 1 :(得分:0)

(cond
  [(even? x) (< 20 x)]
  [else (even? x)])

在另一种语言中,例如ruby,这可以写成:

if x.even?
  x < 20
else
  x.even?
end