Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - Do literal backslashes always have to be escaped in a string?

Tags:

string

haskell

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?

like image 449
Justin Ethier Avatar asked Sep 25 '10 02:09

Justin Ethier


2 Answers

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.)

like image 92
Nathan Shively-Sanders Avatar answered Nov 09 '22 15:11

Nathan Shively-Sanders


There is a nice library (raw-strings-qq) which gives you QuasiQouter

import Text.RawString.QQ

multiline :: String
multiline = [r|<HTML>
<HEAD>
<TITLE>Auto-generated html formated source</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
</HEAD>
<BODY LINK="800080" BGCOLOR="#ffffff">
<P> </P>
<PRE>|]
like image 39
spinus Avatar answered Nov 09 '22 14:11

spinus