haskell

Multiline string concatenation in Haskell


I have a very long string I am building in Haskell

let str = "(\"" ++ (int_list_to_string (printed ctx) "") ++ "\",\"" ++ (int_list_to_string (stack ctx) "") ++ "\")"

This is ugly, and I have been trying to put things on separate lines, but I get an error when I do something like

let str = "(\"" 
    ++ (int_list_to_string (printed ctx) "")
    ++ ...

seemingly regardless of the indentation I put before each line.

How can I write a long string concatenation on separate lines? I'm using GHC if that is important.


Solution

  • seemingly regardless of the indentation I put before each line.

    When brute force doesn't work, it's because you haven't used enough force. Likewise here, you haven't used enough indentation. The expression giving the definition of a variable must be indented by more than that variable's name. Your definitions are indented by the same amount, and so they are not parsed correctly. Even one more space would do:

    let str = "(\"" 
         ++ (int_list_to_string (printed ctx) "")
         ++ ...