tags:

views:

74

answers:

1

In Haskell, in order to represent the literal string "\", one would normally write:

"\\"

However, is there a way to escape the string such that a single backslash can be written by itself without needing to be escaped? For example, I can do exactly this in C# by pre-pending @ to the string:

@"\"

Does Haskell have an equivalent?

+3  A: 

No, see section 2.6 of Haskell Lexical Structure.

Haskell doesn't have raw strings, heredocs or triple strings. Sorry. The only fanciness you get is this:

  "ab\
  \cd"

=> 

"abcd"

White space between line-ending and -beginning slashes is ignored. This is useful, for example, if you want to emit some inline HTML that is properly indented in the Haskell source, but not overly indented in the output. (Not that I'd advocate doing complicated HTML output this way.)

Nathan Sanders
and I'd have to say, the whitespace-ignoring backslashes seem a bit of misfeature to me, what with haskell being fairly easy to constant-fold.
sreservoir
I used the feature once, I can't remember why. It wasn't to emit HTML though.
Nathan Sanders
ideally, the compiler would be able to determine `"blah" ++ "blah"` at compile time in pure code. in reality, I have no idea.
sreservoir
@sreservoir: At least GHC is doing this.
FUZxxl
then all is good.
sreservoir