我如何在Haskell中编写多行字符串? [英] How can I write multiline strings in Haskell?

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

问题描述

Let's say I have this string literal with line breaks:

第一行\第二行\第三行

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

有什么办法可以写这是什么?

Is there any way to write it like this?

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

上面的尝试导致出现此错误:

The attempt just above leads to this error:

factor.hs:58:33:
    lexical error in string/character literal at character '\n'
Failed, modules loaded: none.


推荐答案



You can write multiline strings like so

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

打印为

Which prints as

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

如果您想将其打印为两行

If you want this to print as two lines

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

打印为

Which prints as

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

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

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