“?\s”在 Elixir 中是什么意思?

What does "?\s" mean in Elixir?

在涉及 comprehensions 的 Elixir 文档中,我 运行 遍及以下示例:

iex> for <<c <- " hello world ">>, c != ?\s, into: "", do: <<c>>
"helloworld"

我现在大概理解了整个表达式,但我不明白“?\s”是什么意思。 我知道它以某种方式匹配并因此过滤掉 spaces,但这就是我的理解结束的地方。

编辑:我现在已经弄明白了,它解析为32,也就是一个space的字符编码,但我仍然不知道为什么。

? 是一个文字,它为您提供以下字符的代码点 (https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#utf-8-and-unicode)。对于无法按字面意思表示的字符(space 只是其中之一,但还有更多:tab、carriage return、...)应该使用转义序列代替。所以 ?\s 给你一个代码点 space:

iex> ?\s
32

具有用美元符号表示的字符文字。

Erlang/OTP 22 [erts-10.6.1] [...]

Eshell V10.6.1  (abort with ^G)
1> $\s == 32.
%%⇒ true

has char literals that according to the code documentation 完全一样 erlang char literals:

This is exactly what Erlang does with Erlang char literals ($a).


基本上,?\s 完全相同(问号后跟 space。)

#               ⇓ space here
iex|1 ▶ ?\s == ? 
warning: found ? followed by code point 0x20 (space), please use ?\s instead

?\s 没有什么特别之处,如您所见:

for <<c <- " hello world ">>, c != ?o, into: "", do: <<c>> 
#⇒ " hell wrld "

此外, 也对字符文字使用 ?c 表示法:

main> ?\s == ' '
#⇒ true