Newline in Haskell String?

前端 未结 1 1104
醉梦人生
醉梦人生 2020-12-30 19:24

How can I create a newline inside a String? Is it possible without using IO ()?

formatRow :: Car -> String
formatRow (a, d:ds, c, x:xs) = a +         


        
相关标签:
1条回答
  • 2020-12-30 19:58

    To create a string containing a newline, just write "\n".

    If you run your program on Windows, it will automatically be converted to "\r\n".

    Note that calling show on it will escape the newline (or any other meta-characters), so don't do foo ++ (show "\n") or foo ++ (show '\n') - just use foo ++ "\n".

    Also note that if you just evaluate a string expression in GHCi without using putStr or putStrLn, it will just call show on it, so for example the string "foo\n" will display as "foo\n" in GHCi, but that does not change the fact that it's a string containing a newline and it will print that way, once you output it using putStr.

    0 讨论(0)
提交回复
热议问题