以ML(SMLNJ)打开文件

Saj*_*ani 0 ml sml smlnj readfile

我需要读取ML(SLMNJ)中的文件并将其保存在某些结构中.我需要阅读一些指向图形声明的数据:

[( 1 , 2 , 13 ),( 2 , 3 , 3 ),( 2 , 4 , 8 ),( 2 , 5 , 4 ),( 3 , 1 , 5 ),( 3 , 4 , 1 ),( 4 , 6 , 5 ),( 5 , 5 , 5 ),( 6 , 4 , 6 )]
Run Code Online (Sandbox Code Playgroud)

(第一个数字:节点的名称,第二个数字:连接节点的名称,此鬃毛的第三个数字权重(每个()显示一个鬃毛))

对于expamle,这是测试输入如何读取文件以及保存它的结构

Saj*_*ani 7

从文件读取遵循此行到每行的字符串列表:

val infile = "c:/input.txt" ;

fun readlist (infile : string) = let 

  val ins = TextIO.openIn infile 

  fun loop ins = 

   case TextIO.inputLine ins of 

      SOME line => line :: loop ins 

    | NONE      => [] 

in 

  loop ins before TextIO.closeIn ins 

end ;

val pureGraph =  readlist(infile);
Run Code Online (Sandbox Code Playgroud)

使用此函数,您可以将其解析为元组(x,y,z):

fun creatGraph([],reList) = reList

|creatGraph(x::y::z::input,reList) =  creatGraph(input,reList@[(x,y,z)]);
Run Code Online (Sandbox Code Playgroud)

  • 提供答案的原始来源会很好.http://stackoverflow.com/a/742549/2339141 (2认同)