Multi-line strings in Haskell

- February 6, 2014
Kwang's Haskell Blog - Multi-line strings in Haskell

Multi-line strings in Haskell

Posted on February 6, 2014 by Kwang Yul Seo

Haskell supports multi-line string literals in several ways.

unlines

unlines joins lines, after appending a terminating newline to each.

multi = unlines ["line1", "line2", "line3"]

Multi-line string literal

We should escape it using \ and then another \ where the string starts again.

multi = "line1\
\line2\
\line3"

Quasiquotation

The raw-strings-qq package provides a quasiquoter for raw string literals. In addition to supporting multi-line string, it does not recognize escape sequences. So we don’t need to add \ as in multi-line string literals. {-# LANGUAGE QuasiQuotes #-} is required to use this feature.

{-# LANGUAGE QuasiQuotes #-}
import Text.RawString.QQ
multi = [r|line1
line2
line3|]

I prefer quasiquotation because I use multi-line string literals for HTML/XML fragments and it is laborious to escape all special characters such as quotes.

{-# LANGUAGE QuasiQuotes #-}
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>|]

There are other quasi-quote packages such as string-qq, string-quote and interpolatedstring-qq.

Read more