最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Multiline string concatenation in Haskell - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Mar 30 at 2:56 donotread123donotread123 1238 bronze badges 4
  • 2 "I get an error" - which error are you getting? – Bergi Commented Mar 30 at 3:16
  • 2 I don't agree with either of the close votes. This is easily reproducible, and not caused by a typo but a misunderstanding of the syntax rules. No further "debugging information" is necessary - you can see the problem just by looking at the code for a few seconds, if you are familiar with Haskell's layout rules. – amalloy Commented Mar 30 at 6:46
  • 2 That said, it would be a much better question if you included at least the compiler error message, ideally with a code snippet that could be pasted as-is into a new Haskell file without needing to replace all your variables with placeholders. – amalloy Commented Mar 30 at 6:59
  • @amalloy Debugging questions are required to include a minimal reproducible example which includes the error message encountered. This is because someone could potentially have a different error even with the same code, depending on other environment variables, etc. Without knowing the error that OP is encountering, we're relying on chance that every other variable in the environment is the same. Since you commented. – TylerH Commented Mar 31 at 17:42
Add a comment  | 

3 Answers 3

Reset to default 4

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

You don't need to define the entire value as one long expression. Take advantage of the ability to define multiple names in a single let construct. Some of the can even be helper functions to make it easier to see what you are actually constructing. For example,

let printedStr = int_list_to_string (printed ctx) ""
    stackStr = int_list_to_string (stack ctx) ""
    quote s = "\"" + s + "\""
    str = "(" ++ quote printedStr ++ "," ++ quote stackStr + ")"
   

There is an underlying structure to the string you are building; express that structure in your code. Specifically, "associate" the quotation marks with the converted lists, not the parentheses and commas.

As others have pointed out, the main issue here is indentation.

I want to point out that the Show instance of a tuple of strings would be an easy solution here, as it automatically provides the parentheses, comma, and escaped quotation marks.

str = show (int_list_to_string (printed ctx) "", int_list_to_string (stack ctx) "")

If there is nothing special about creating strings from ctx int lists, one might make this even simpler as: str = show (show (printed ctx), show (stack ctx)).

~~~~~

Assume:

int_list_to_string :: [Int] -> String -> String
int_list_to_string xs _ = show xs
str = "(\"" ++ (int_list_to_string [1,2,3] "") ++ "\",\"" ++ (int_list_to_string [4,5,6] "") ++ "\")"
str' = show (int_list_to_string [1,2,3] "", int_list_to_string [4,5,6] "")
str'' = show (show [1,2,3], show [4,5,6])
test0 = all (== "(\"[1,2,3]\",\"[4,5,6]\")") [str, str', str'']
发布评论

评论列表(0)

  1. 暂无评论