Haskell - 字面反斜杠总是必须在字符串中转义吗? [英] Haskell - Do literal backslashes always have to be escaped in a string?

查看:314
本文介绍了Haskell - 字面反斜杠总是必须在字符串中转义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Haskell中,为了表示字符串\,通常会写入:

 \\

然而,有没有方式来逃避字符串,使单个反斜杠可以自己写,而不需要逃脱?例如,我可以在C#中通过预处理 @ 来完成以下操作:

  @\

Haskell具有相同的功能吗?

解决方案

不,详见 Haskell词汇结构

Haskell没有原始字符串,heredocs或三重字符串。抱歉。您唯一的幻想是这样的:

 ab\ 
\cd

=>

abcd

行结束和开始斜线之间的空白被忽略。例如,如果您想要发出一些在Haskell源代码中正确缩进但不会在输出中过度缩进的内联HTML,这很有用。 (不是我主张以这种方式做复杂的HTML输出。)


In Haskell, in order to represent the literal string "\", one would normally write:

"\\"

However, is there a way to escape the string such that a single backslash can be written by itself without needing to be escaped? For example, I can do exactly this in C# by pre-pending @ to the string:

@"\"

Does Haskell have an equivalent?

解决方案

No, see section 2.6 of Haskell Lexical Structure.

Haskell doesn't have raw strings, heredocs or triple strings. Sorry. The only fanciness you get is this:

  "ab\
  \cd"

=> 

"abcd"

White space between line-ending and -beginning slashes is ignored. This is useful, for example, if you want to emit some inline HTML that is properly indented in the Haskell source, but not overly indented in the output. (Not that I'd advocate doing complicated HTML output this way.)

这篇关于Haskell - 字面反斜杠总是必须在字符串中转义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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