Haskell字符串中的换行符? [英] Newline in Haskell String?

查看:349
本文介绍了Haskell字符串中的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在String中创建一个换行符?是否可以不使用 IO()

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 ++ " | " ++ x ++ concat xs ++ " | " ++ show c ++ " | " ++ d ++ concat ds ++ (show '\n')


推荐答案

要创建一个包含换行符的字符串,只需写\\\

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

show 就会跳过换行符(或任何其他元字符),所以不要执行 foo ++(show\\\
foo ++(show'\\\
')
- 只需使用 foo ++\\\

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

另请注意,如果您只是在不使用 putStr 的情况下评估ghci中的字符串表达式>或 putStrLn ,它只会对它调用 show ,例如字符串 foo \ n在ghci中会显示为foo \ n,但这并不改变它是一个包含新行,它将以这种方式打印,一旦你使用 putStr 输出它。

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.

这篇关于Haskell字符串中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆