使用 SML 将字符串转换为字符列表

发布于 2024-09-27 04:01:33 字数 1680 浏览 0 评论 0原文

我正在尝试将字符串转换为字符列表列表,并且我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鯉魚旗 2024-10-04 04:01:33

你可以使用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

不顾 2024-10-04 04:01:33

您需要找到或编写 lines 函数。它接受一个字符串,并根据换行符出现的位置将其分解为字符串数组。

它在 haskell 中被称为lines。除此之外;您必须在累积字符串数组时逐行输入,而不是 inutAll

另外,您输入的文件似乎具有实际的 '\' \n' 字符而不是换行符。

输入应该是:

53**7****
6**195***
*98****6*
8***6***3
4**8*3**1
7***2***6
*6****28*
***419**5
****8**79

lines.sml:

open Char;
open String;
open List;

fun linelist file =
    let val instr = TextIO.openIn file
        val str   = TextIO.inputAll instr
    in tokens isSpace str
       before
       TextIO.closeIn instr
    end;


fun getsudo file   = map explode (linelist file);


fun  main args = 
   getsudo "sudo.txt";

会话:

- main 1;
val it =
  [[#"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"]] : char list list
- 

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 of inutAll, 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:

53**7****
6**195***
*98****6*
8***6***3
4**8*3**1
7***2***6
*6****28*
***419**5
****8**79

lines.sml:

open Char;
open String;
open List;

fun linelist file =
    let val instr = TextIO.openIn file
        val str   = TextIO.inputAll instr
    in tokens isSpace str
       before
       TextIO.closeIn instr
    end;


fun getsudo file   = map explode (linelist file);


fun  main args = 
   getsudo "sudo.txt";

Session:

- main 1;
val it =
  [[#"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"]] : char list list
- 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文