Injecting long lines without using lists

For multiple projects, I need to embed long lines in the Haskell source code.

The obvious way to do this is with a unlineslist of strings. However, reading and maintaining this is cumbersome.

cCode :: String
cCode = unlines [
          "int main(int argc*, char** argv)",
          "  doStuff();",
          "}"]

Is it possible to embed strings without any overhead (for example, a list as shown above) or even files? Is a TemplateHaskell / Quasi-quot quote how to go here?

Note: This question was given in Q & A. Therefore, it does not show any studies.

+4
source share
2 answers

QuasiQuotation, blogpost, .

1: ( StringEmbed.hs,

module StringEmbed(embedStr, embedStrFile) where

import Language.Haskell.TH
import Language.Haskell.TH.Quote

embedStr :: QuasiQuoter 
embedStr = QuasiQuoter { quoteExp = stringE,
                    quotePat = undefined,
                    quoteDec = undefined,
                    quoteType = undefined }

embedStrFile :: QuasiQuoter
embedStrFile = quoteFile embedStr

, - TH , .

2a: :

{-# LANGUAGE QuasiQuotes #-}

import StringEmbed

cCode :: String
cCode = [embedStr|
int main(int argc, char** argv) {
    doStuff();
}
|]

, QuasiQuotes LANGUAGE. TemplateHaskell .

QuasiQuotes |], .

2b: . , code.c , .

{-# LANGUAGE QuasiQuotes #-}

import StringEmbed

cFooter :: String
cFooter = [embedStrFile|code.c|]

haskell StringEmbed.hs, heredoc ( Ørjan Johansen !)

{-# LANGUAGE QuasiQuotes #-}
import Text.Heredoc


cCode :: String
cCode = [here|
int main(int argc, char** argv) {
    doStuff();
}
|]
+5

. , , Haskell.

cCode :: String
cCode = "\
      \int main(int argc*, char** argv)\n\
      \  doStuff();\n\
      \}\n\
      \"
+3

All Articles