如何在Haskell中编写多行字符串?

liubiantao :

假设我有带换行符的字符串文字:

file :: String
file = "the first line\nthe second line\nthe third line"

有什么办法可以这样写吗?

file :: String
file = "the first line
        the second line
        the third line"

上面的尝试导致此错误:

factor.hs:58:33:
    lexical error in string/character literal at character '\n'
Failed, modules loaded: none.
丹尼尔·格拉泽(Daniel Gratzer):

您可以像这样编写多行字符串

x = "This is some text which we escape \
      \   and unescape to keep writing"

哪个打印为

"This is some text which we escape   and unescape to keep writing"

如果要将此打印为两行

x = "This is some text which we escape \n\
      \   and unescape to keep writing"

哪个打印为

This is some text which we escape
    and unescape to keep writing

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章