Turning a string into a char list list using SML

混江龙づ霸主 提交于 2019-12-07 08:30:27

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
- 

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!