简体   繁体   English

逃脱的&符号在Haskell中意味着什么?

[英]What does an escaped ampersand mean in Haskell?

I looked at the Haskell 2010 report and noticed a weird escape sequence with an ampersand: \\& . 我查看了Haskell 2010报告,发现了一个带有&符号的奇怪的转义序列: \\& I couldn't find an explanation what this escape sequence should stand for. 我无法找到解释这个逃逸序列应该代表什么的解释。 It also might only be located in strings. 它也可能只位于字符串中。 I tried print "\\&" in GHCi, and it prints an empty string. 我尝试在GHCi中print "\\&" ,然后打印一个空字符串。

It escapes... no character. 它逃脱......没有性格。 It is useful to "break" some escape sequences. “破坏”一些转义序列很有用。 For instance we might want to express "\\12" ++ "3" as a single string literal. 例如,我们可能希望将"\\12" ++ "3"为单个字符串文字。 If we try the obvious approach, we get 如果我们尝试明显的方法,我们得到

"\123" ==> "{"

We can however use 但是我们可以使用

"\12\&3"

for the intended result. 为了预期的结果。

Also, "\\SOH" and "\\SO" are both valid single ASCII character escapes, making "\\SO" ++ "H" tricky to express as a single literal: we need "\\SO\\&H" for that. 此外, "\\SOH""\\SO"都是有效的单个ASCII字符转义,使"\\SO" ++ "H"难以表达为单个文字:我们需要"\\SO\\&H"

This escape trick is also exploited by the standard Show String instance, which has to produce a valid literal syntax. 标准的Show String实例也利用了这个转义技巧,该实例必须生成有效的文字语法。 We can see this in action in GHCi: 我们可以在GHCi中看到这一点:

> "\140" ++ "0"
"\140\&0"
> "\SO" ++ "H"
"\SO\&H"

Further, this greatly helps external programs which aim to generate Haskell code (eg for metaprogramming). 此外,这极大地帮助了旨在生成Haskell代码的外部程序(例如,用于元编程)。 When emitting characters for a string literal, the external program can add \\& at the end of potentially ambiguous escapes (or even of all escapes) so that the program does not have to handle unwanted interactions. 当为字符串文字发出字符时,外部程序可以在可能不明确的转义(甚至是所有转义)的末尾添加\\& ,以便程序不必处理不需要的交互。 Eg if the program wants to emit \\12 now, it can emit \\12\\& and be free to emit anything as the next character. 例如,如果程序想要现在发出\\12 ,它可以发出\\12\\&并且可以自由地发出任何内容作为下一个字符。 Otherwise, the program should remember that, when the next character is emitted, it has to be prepended by \\& if it's a digit. 否则,程序应该记住,当发出下一个字符时,它必须由\\&前缀,如果它是一个数字。 It's simpler to always add \\& , even if it's not needed: \\12\\&A is legal, and has the same meaning as \\12A . 即使不需要,也总是添加\\&更简单: \\12\\&A是合法的,与\\12A具有相同的含义。

Finally, a quote from the Haskell Report, explaining \\& : 最后,引用Haskell报告,解释\\&

2.6 Character and String Literals 2.6字符和字符串文字

[...] [...]

Consistent with the "maximal munch" rule, numeric escape characters in strings consist of all consecutive digits and may be of arbitrary length. 与“最大munch”规则一致,字符串中的数字转义字符由所有连续数字组成,并且可以是任意长度。 Similarly, the one ambiguous ASCII escape code, "\\SOH" , is parsed as a string of length 1. The escape character \\& is provided as a "null character" to allow strings such as "\\137\\&9" and "\\SO\\&H" to be constructed (both of length two). 类似地,一个不明确的ASCII转义码"\\SOH"被解析为长度为1的字符串。转义字符\\&被提供为"null character"以允许诸如"\\137\\&9""\\SO\\&H" "\\137\\&9"字符串"\\SO\\&H"将被构建(两个长度均为2)。 Thus "\\&" is equivalent to "" and the character '\\&' is disallowed. 因此"\\&"等同于""并且不允许使用字符'\\&' Further equivalences of characters are defined in Section 6.1.2. 第6.1.2节定义了字符的进一步等价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM