使用 SML 将字符串转换为字符列表
我正在尝试将字符串转换为字符列表列表,并且我有以下代码:
fun linelist file =
let
val instr = TextIO.openIn file
val str = TextIO.inputAll instr
in
String.tokens (fn x => x = #"\n")str
before
TextIO.closeIn instr
end;
fun getsudo file = map explode (linelist file);
我需要将包含以下内容的文件(sudo.txt)转换
53**7****\n6**195***\n*98****6*\n8***6***3\n4**8*3**1\n7***2***6\n*6****28*\n***419**5\n****8**79\n
为
[[#"5",#"3",#"*",#"*",#"7",#"*",#"*",#"*",#"*",],
[#"6",#"*",#"*",#"1",#"9",#"5",#"*",#"*",#"*",],
[#"*",#"9",#"8",#"*",#"*",#"*",#"*",#"6",#"*",],
[#"8",#"*",#"*",#"*",#"6",#"*",#"*",#"*",#"3",],
[#"4",#"*",#"*",#"8",#"*",#"3",#"*",#"*",#"1",],
[#"7",#"*",#"*",#"*",#"2",#"*",#"*",#"*",#"6",],
[#"*",#"6",#"*",#"*",#"*",#"*",#"2",#"8",#"*",],
[#"*",#"*",#"*",#"4",#"1",#"9",#"*",#"*",#"5",],
[#"*",#"*",#"*",#"*",#"8",#"*",#"*",#"7",#"9",]]
但我得到了这个
[[#"5",#"3",#"*",#"*",#"7",#"*",#"*",#"*",#"*",#"\\",#"n",#"6",
#"*",#"*",#"1",#"9",#"5",#"*",#"*",#"*",#"\\",#"n",#"*",#"9",
#"8",#"*",#"*",#"*",#"*",#"6",#"*",#"\\",#"n"#"8",#"*",#"*",
#"*",#"6",#"*",#"*",#"*",#"3",#"\\",#"n",#"4",#"*",#"*",#"8",
#"*",#"3",#"*",#"*",#"1",#"\\",#"n",#"7",#"*",#"*",#"*",#"2",
#"*",#"*",#"*",#"6",#"\\",#"n",#"*",#"6",#"*",#"*",#"*",#"*",
#"2",#"8",#"*",#"\\",#"n",#"*",#"*",#"*",#"4",#"1",#"9",#"*",
#"*",#"5",#"\\",#"n",#"*",#"*",#"*",#"*",#"8",#"*",#"*",#"7",
#"9",#"\\",#"n"]]
如何修复它?
I'm trying to turn a string into a char list list and I have the following code:
fun linelist file =
let
val instr = TextIO.openIn file
val str = TextIO.inputAll instr
in
String.tokens (fn x => x = #"\n")str
before
TextIO.closeIn instr
end;
fun getsudo file = map explode (linelist file);
I need to turn a file (sudo.txt) with the following
53**7****\n6**195***\n*98****6*\n8***6***3\n4**8*3**1\n7***2***6\n*6****28*\n***419**5\n****8**79\n
into
[[#"5",#"3",#"*",#"*",#"7",#"*",#"*",#"*",#"*",],
[#"6",#"*",#"*",#"1",#"9",#"5",#"*",#"*",#"*",],
[#"*",#"9",#"8",#"*",#"*",#"*",#"*",#"6",#"*",],
[#"8",#"*",#"*",#"*",#"6",#"*",#"*",#"*",#"3",],
[#"4",#"*",#"*",#"8",#"*",#"3",#"*",#"*",#"1",],
[#"7",#"*",#"*",#"*",#"2",#"*",#"*",#"*",#"6",],
[#"*",#"6",#"*",#"*",#"*",#"*",#"2",#"8",#"*",],
[#"*",#"*",#"*",#"4",#"1",#"9",#"*",#"*",#"5",],
[#"*",#"*",#"*",#"*",#"8",#"*",#"*",#"7",#"9",]]
but I get this instead
[[#"5",#"3",#"*",#"*",#"7",#"*",#"*",#"*",#"*",#"\\",#"n",#"6",
#"*",#"*",#"1",#"9",#"5",#"*",#"*",#"*",#"\\",#"n",#"*",#"9",
#"8",#"*",#"*",#"*",#"*",#"6",#"*",#"\\",#"n"#"8",#"*",#"*",
#"*",#"6",#"*",#"*",#"*",#"3",#"\\",#"n",#"4",#"*",#"*",#"8",
#"*",#"3",#"*",#"*",#"1",#"\\",#"n",#"7",#"*",#"*",#"*",#"2",
#"*",#"*",#"*",#"6",#"\\",#"n",#"*",#"6",#"*",#"*",#"*",#"*",
#"2",#"8",#"*",#"\\",#"n",#"*",#"*",#"*",#"4",#"1",#"9",#"*",
#"*",#"5",#"\\",#"n",#"*",#"*",#"*",#"*",#"8",#"*",#"*",#"7",
#"9",#"\\",#"n"]]
How do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以使用explode()。这会将字符串列表转换为字符列表,这就是您想要做的。调用explode('string')进行转换
you can use explode(). This turns a string list to a char list, which is what youre trying to do. call explode('string') to convert
您需要找到或编写
lines
函数。它接受一个字符串,并根据换行符出现的位置将其分解为字符串数组。它在 haskell 中被称为
lines
。除此之外;您必须在累积字符串数组时逐行输入,而不是inutAll
。另外,您输入的文件似乎具有实际的 '\' \n' 字符而不是换行符。
输入应该是:
lines.sml:
会话:
You need to locate or write the
lines
function. It takes a string and break it into an array of strings according to where the Newline characters occur.It's called
lines
in haskell. Other than that; instead ofinutAll
, you'll have to input line-by-line while accumulating an array of strings.Also, it appears you input file has the actual '\' \n' characters instead of newlines.
The input should be:
lines.sml:
Session: