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.
Related Questions
Sponsored Content
20 Answered Questions
[SOLVED] What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
- 2008-08-31 15:04:35
- Todd
- 653588 View
- 2159 Score
- 20 Answer
- Tags: python syntax parameter-passing variadic-functions argument-unpacking
27 Answered Questions
[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?
- 2009-08-26 16:10:13
- Mark Rogers
- 1046457 View
- 7345 Score
- 27 Answer
- Tags: javascript syntax jslint use-strict
3 Answered Questions
[SOLVED] What does the exclamation mark mean in a Haskell declaration?
- 2009-06-14 16:07:47
- David
- 25587 View
- 247 Score
- 3 Answer
- Tags: haskell syntax lazy-evaluation
44 Answered Questions
[SOLVED] What is the preferred syntax for defining enums in JavaScript?
- 2008-11-13 19:09:06
- David Citron
- 948562 View
- 1990 Score
- 44 Answer
- Tags: javascript syntax enums
5 Answered Questions
[SOLVED] What does the star operator mean, in a function call?
- 2010-05-27 14:10:38
- psihodelia
- 168410 View
- 559 Score
- 5 Answer
- Tags: python syntax parameter-passing iterable-unpacking argument-unpacking
18 Answered Questions
45 Answered Questions
[SOLVED] What is a monad?
- 2008-09-04 23:26:44
- ljs
- 233288 View
- 1374 Score
- 45 Answer
- Tags: haskell functional-programming monads terminology
9 Answered Questions
[SOLVED] Why does ++[[]][+[]]+[+[]] return the string "10"?
- 2011-08-26 08:46:14
- JohnJohnGa
- 198084 View
- 1616 Score
- 9 Answer
- Tags: javascript syntax
9 Answered Questions
15 Answered Questions
[SOLVED] Getting started with Haskell
- 2009-06-18 13:17:11
- anderstornvig
- 242685 View
- 755 Score
- 15 Answer
- Tags: haskell functional-programming
1 comments
@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 getWe 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: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
\&
:@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.