简体   繁体   English

\n 如何在 haskell 的字符列表中工作

[英]How does \n work in Char lists in haskell

right now I have a String equal to "...\n...\n..." .现在我有一个字符串等于"...\n...\n..." In my code I want to write this as a list (like ['a','b','c'] ), but how would this work with the \n?在我的代码中,我想把它写成一个列表(比如['a','b','c'] ),但是这如何与 \n 一起工作? I checked in ghci if string == ['.','.','.','.','.','.','.','.','.'] and it said no, so does anyone know how I would write the \n's in a Char list, thank you.我检查了 ghci if string == ['.','.','.','.','.','.','.','.','.']它说不,有谁知道我将如何在字符列表中写 \n,谢谢。

A String is a list of Char acters, so "foo" and ['f', 'o', 'o'] are exactly the same. StringChar角色的列表,因此"foo"['f', 'o', 'o']完全相同

For a new line character '\n' [wiki] you can escape this, so your string "...\n...\n..." is equivalent to:对于换行符'\n' [wiki] ,您可以对其进行转义,因此您的字符串"...\n...\n..."等效于:

['.', '.', '.', '\n', '.', '.', '.', '\n', '.', '.', '.']

Here '\n' is a single character, not two : it maps to an ASCII character with codepoint 0a as hexadecimal value ( 10 as decimal value).这里'\n'一个字符,而不是两个:它映射到一个 ASCII 字符,代码点0a作为十六进制值( 10作为十进制值)。 The compiler thus sees \n and replaces that with a single character.因此,编译器看到\n并将其替换为单个字符。

You can thus for example filter with filter ('\n' /=) some_string to filter out new line characters from a String .例如,您可以使用filter ('\n' /=) some_string进行过滤,以从String中过滤掉换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM