Syntax 条件表达式及其语法和计算
有人能澄清为什么程序返回22吗?很明显,这个语句写得不正确,但无论如何,为什么它返回22 先谢谢你Syntax 条件表达式及其语法和计算,syntax,scheme,conditional,evaluation,Syntax,Scheme,Conditional,Evaluation,有人能澄清为什么程序返回22吗?很明显,这个语句写得不正确,但无论如何,为什么它返回22 先谢谢你 (define (proc x) (cond (= x 22) (display 3) (display 4) ) ) (proc 3) 你忘了几个括号。在此代码中: (define (proc x) (cond (= x 22) (display 3)
(define (proc x)
(cond (= x 22)
(display 3)
(display 4)
)
)
(proc 3)
你忘了几个括号。在此代码中:
(define (proc x)
(cond (= x 22)
(display 3)
(display 4)))
您正在测试=
过程是否是真的
——它是真的,因为在Scheme中,除了#f
之外,一切都是真实的,它是假的
。假设=
为true
,则返回与该条件对应的最后一个值,在本例中:22
。cond
的正确语法如下:
(define (proc x)
(cond ((= x 22)
(display 3)
(display 4))
(else 'error)))
(proc 22)
=> 34
(proc 3)
=> 'error
你忘了几个括号。在此代码中:
(define (proc x)
(cond (= x 22)
(display 3)
(display 4)))
您正在测试=
过程是否是真的
——它是真的,因为在Scheme中,除了#f
之外,一切都是真实的,它是假的
。假设=
为true
,则返回与该条件对应的最后一个值,在本例中:22
。cond
的正确语法如下:
(define (proc x)
(cond ((= x 22)
(display 3)
(display 4))
(else 'error)))
(proc 22)
=> 34
(proc 3)
=> 'error
在第一个cond子句中,
=
不是#f
(它是一个基本运算符),因此它被认为是真的,#t
,然后依次计算x
和22
,最终结果是22
。在第一个cond子句中,=/code>不是#f
(它是一个基本运算符),因此它被认为是正确的,#t
,然后依次计算x
和22
,最终结果是22
。