Haskell字符串中的换行符?

Ash*_*Ash 31 string haskell newline

如何在String中创建换行符?没有使用可能IO ()吗?

formatRow :: Car -> String
formatRow (a, d:ds, c, x:xs) = a ++ " | " ++ x ++ concat xs ++ " | " ++ show c ++ " | " ++ d ++ concat ds ++ (show '\n')
Run Code Online (Sandbox Code Playgroud)

sep*_*p2k 58

要创建包含换行符的字符串,只需写入"\n".

请注意,调用"\r\n"它将转义换行符(或任何其他元字符),所以不要这样做- show或者foo ++ (show "\n")只是使用foo ++ (show '\n').

另请注意,如果你只是在不使用foo ++ "\n"or来评估ghci中的字符串表达式putStr,它只会调用putStrLn它,所以例如字符串show将显示为"foo\n"ghci,但这并不会改变它是包含换行符的字符串这一事实一旦你输出它将以这种方式打印"foo\n".

  • 感谢`putStr`,对我这样的绝对新手非常有用,因为我实际上搜索'为什么我的新行不会显示在ghci中,因为我希望他们' (5认同)
  • 或者使用'unlines' (3认同)
  • 所以我应该改用 putStrLn ,这会正确显示吗? (2认同)