How can I write multiline strings in Haskell?

Haskell

Haskell Problem Overview


Let's say I have this string literal with line breaks:

file :: String
file = "the first line\nthe second line\nthe third line"

Is there any way to write it like this?

file :: String
file = "the first line
        the second line
        the third line"

The attempt just above leads to this error:

factor.hs:58:33:
    lexical error in string/character literal at character '\n'
Failed, modules loaded: none.

Haskell Solutions


Solution 1 - Haskell

You can write multiline strings like so

x = "This is some text which we escape \
      \   and unescape to keep writing"

Which prints as

"This is some text which we escape   and unescape to keep writing"

If you want this to print as two lines

x = "This is some text which we escape \n\
      \   and unescape to keep writing"

Which prints as

This is some text which we escape
    and unescape to keep writing

Solution 2 - Haskell

Some time ago I released a library named "neat-interpolation" to solve the problem of multiline strings and interpolation using the QuasiQoutes extension. Its primary advantage over competitors is a smart management of whitespace, which takes care of interpolated multiline strings. Following is an example of how it works.

Executing the following:

{-# LANGUAGE QuasiQuotes, OverloadedStrings #-}

import NeatInterpolation (text)
import qualified Data.Text.IO

f :: Text -> Text -> Text
f a b = 
  [text|
    function(){
      function(){
        $a
      }
      return $b
    }
  |]

main = Data.Text.IO.putStrLn $ f "1" "2"

will produce this (notice the reduced indentation compared to how it was declared):

function(){
  function(){
    1
  }
  return 2
}

Now let's test it with multiline string parameters:

main = Data.Text.IO.putStrLn $ f 
  "{\n\
  \  indented line\n\
  \  indented line\n\
  \}" 
  "{\n\
  \  indented line\n\
  \  indented line\n\
  \}"

We get

function(){
  function(){
    {
      indented line
      indented line
    }
  }
  return {
    indented line
    indented line
  }
}

Notice how it neatly preserved the indentation levels of lines the variable placeholders were at. The standard interpolators would have messed all the whitespace and produced something like the following instead:

    function(){
      function(){
        {
  indented line
  indented line
}
      }
      return {
  indented line
  indented line
}
    }

Solution 3 - Haskell

In Haskell you can type multiline strings by ending it with a backslash \ and beginning the newline with another \, just like so :

file :: String
file = "the first line\n\  
    \the second line\n\  
    \the third line\n"  

Solution 4 - Haskell

I don't believe that Haskell has an easy way to do this without resorting to quasiquoting or something else. However you can mostly get what you want by using the unlines function like below. However this will result in a newline after your last line, which you may or may not care about.

file :: String
file = unlines [
    "the first line",
    "the second line",
    "the third line"
    ]

Solution 5 - Haskell

Many times this facality is referred to as heredocs. Haskell does not have heredocs.

However, there are several packages that use GHC's QuasiQuotes extensions to achieve this.

Here is one I use: http://hackage.haskell.org/package/interpolatedstring-perl6-0.9.0/docs/Text-InterpolatedString-Perl6.html

Your example would look like:

file = [q|
the first line
the second line
the third line
|]

Solution 6 - Haskell

Quasiquotation examplete presented in Multi-line strings in Haskell.

{-# 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>|]

raw-strings-qq is my favorite for the purpose yet.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionliubiantaoView Question on Stackoverflow
Solution 1 - HaskellDaniel GratzerView Answer on Stackoverflow
Solution 2 - HaskellNikita VolkovView Answer on Stackoverflow
Solution 3 - HaskellnschoeView Answer on Stackoverflow
Solution 4 - HaskellVarun MadiathView Answer on Stackoverflow
Solution 5 - HaskellJonathan FischoffView Answer on Stackoverflow
Solution 6 - HaskellpalikView Answer on Stackoverflow