添加括号会改变条件的评估
Adding parenthesis changes evaluation of condition
我开始学习 Scheme 并偶然发现了这个奇怪的东西:
给定以下程序:
(define (test x y) (if (= x 0) 0 y ))
当我创建一个条件时,它会在我向其添加括号时计算 "as expected":(test 0 1)
给出 0。但是当我不添加括号时(并且我使用相同的输入)它评估为假条件:test 0 1
给出 1.
这是为什么?
如果你写:
test 0 1
这等同于:
(begin
test ; evaluate variable test => #<procedure:something...> or similar value
0 ; evaluate literal 0 => 0
1) ; evaluate literal 1 => 1
==> 1 ; because 1 is the last expression in the block it is the result of it.
当您执行 (test 0 1)
时,您将通过使用两个参数 0
和 1
评估变量 test
来调用您将获得的过程他们代表的数字。如果您进行替换,它将变为:
(if (= 0 0) ; if 0 is the same numeric value as 0
0 ; then evaluate 0
1) ; else evaluate 1
==> 0
同JavaScript:
const test = (x, y) => x === 0 ? 0 : y;
test;0;1
==> 1 // result in node is 1 since it was the last expression
test(0, 1); // calling the fucntion behind the variable test with the arguments 0 and 1
==> 0
所以括号对周围的东西很重要,因为它们对 JavaScript 中的东西很重要。基本上 Scheme 中的 (one two three)
是 JS 中的 one(two, three)
。只是在 somtheing 周围添加括号就是在 JS 中的某些内容之后添加 ()
。
我开始学习 Scheme 并偶然发现了这个奇怪的东西: 给定以下程序:
(define (test x y) (if (= x 0) 0 y ))
当我创建一个条件时,它会在我向其添加括号时计算 "as expected":(test 0 1)
给出 0。但是当我不添加括号时(并且我使用相同的输入)它评估为假条件:test 0 1
给出 1.
这是为什么?
如果你写:
test 0 1
这等同于:
(begin
test ; evaluate variable test => #<procedure:something...> or similar value
0 ; evaluate literal 0 => 0
1) ; evaluate literal 1 => 1
==> 1 ; because 1 is the last expression in the block it is the result of it.
当您执行 (test 0 1)
时,您将通过使用两个参数 0
和 1
评估变量 test
来调用您将获得的过程他们代表的数字。如果您进行替换,它将变为:
(if (= 0 0) ; if 0 is the same numeric value as 0
0 ; then evaluate 0
1) ; else evaluate 1
==> 0
同JavaScript:
const test = (x, y) => x === 0 ? 0 : y;
test;0;1
==> 1 // result in node is 1 since it was the last expression
test(0, 1); // calling the fucntion behind the variable test with the arguments 0 and 1
==> 0
所以括号对周围的东西很重要,因为它们对 JavaScript 中的东西很重要。基本上 Scheme 中的 (one two three)
是 JS 中的 one(two, three)
。只是在 somtheing 周围添加括号就是在 JS 中的某些内容之后添加 ()
。