为什么在 case 表达式中忽略单引号?

Why single quote is ignored in case expression?

(case 'x
   [(x) "ex"]
   [('x) "quoted ex"])

我以为输出会是"quoted ex",但实际上是"ex"。为什么 'x 匹配 x 但不匹配 'x

在第一种情况下,(x) 已经 引用,来自 documentation(强调我的):

The selected clause is the first one with a datum whose quoted form is equal? to the result of val-expr.

因此,('x) 是双引号。如果你想匹配第二种情况,这样的东西是必要的:

(case ''x
  [(x) "ex"]
  [('x) "quoted ex"])

=> "quoted ex"

换句话说,代码中的第一个条件是测试 if (equal? 'x 'x),第二个条件测试 (equal? 'x ''x),显然只有第一个匹配。