By Nolan

2019-07-09 22:45:33 8 Comments

I looked at the Haskell 2010 report and noticed a weird escape sequence with an ampersand: \&. 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.


@chi 2019-07-09 22:51:48

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. If we try the obvious approach, we get

"\123" ==> "{"

We can however use


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.

This escape trick is also exploited by the standard Show String instance, which has to produce a valid literal syntax. We can see this in action in GHCi:

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

Further, this greatly helps external programs which aim to generate Haskell code (e.g. for metaprogramming). 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. E.g. if the program wants to emit \12 now, it can emit \12\& and be free to emit anything as the next character. 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.

Finally, a quote from the Haskell Report, explaining \&:

2.6 Character and String Literals


Consistent with the "maximal munch" rule, numeric escape characters in strings consist of all consecutive digits and may be of arbitrary length. 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). Thus "\&" is equivalent to "" and the character '\&' is disallowed. Further equivalences of characters are defined in Section 6.1.2.

@Jon Purdy 2019-07-09 23:29:16

It’s also the zero-width equivalent of a gap: a backslash followed by some whitespace and another backslash is stripped from the string to allow multi-line string literals, but with no intervening whitespace this gives a single backslash. I’ve found it useful to help syntax highlighters that get tripped up on gaps at the end of a string, since the literal ends with \" but that’s not an escaped quotation mark.

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

45 Answered Questions

[SOLVED] What is a monad?

45 Answered Questions

[SOLVED] What is the preferred syntax for defining enums in JavaScript?

18 Answered Questions

[SOLVED] What does "static" mean in C?

  • 2009-02-21 06:47:52
  • David
  • 819130 View
  • 1001 Score
  • 18 Answer
  • Tags:   c syntax static

15 Answered Questions

[SOLVED] Getting started with Haskell

5 Answered Questions

[SOLVED] What does the star operator mean?

9 Answered Questions

[SOLVED] Why does ++[[]][+[]]+[+[]] return the string "10"?

  • 2011-08-26 08:46:14
  • JohnJohnGa
  • 193545 View
  • 1576 Score
  • 9 Answer
  • Tags:   javascript syntax

9 Answered Questions

[SOLVED] What characters do I need to escape in XML documents?

3 Answered Questions

[SOLVED] What does the exclamation mark mean in a Haskell declaration?

Sponsored Content