在 ML(SMLNJ) 中打开文件 [英] Open file in ML(SMLNJ)
本文介绍了在 ML(SMLNJ) 中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 ML (SLMNJ) 中读取文件并将其保存在某些结构中.我需要读取一些指向图声明的数据:
I need to read file in ML (SLMNJ) and save it in some structures. I need to read some data that points to graph declaration:
[( 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 )]
(第一个数字:节点名称,第二个数字:连接节点名称,第三个数字权重为这个鬃毛(每个()显示一个鬃毛))
(first number: name of the node , secend number: name of connected node , third number weight for this mane (each () show one mane ) )
例如,这是测试输入如何读取文件以及保存文件的结构
for expamle this is test input how to read file and which structure to save it
推荐答案
对于从文件中读取,请按照此到每行字符串列表:
for reading from file follow this to list of string per line :
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);
并且使用此函数您可以将其解析为元组 (x,y,z) :
and with this function you can parse it to tuple (x,y,z ) :
fun creatGraph([],reList) = reList
|creatGraph(x::y::z::input,reList) = creatGraph(input,reList@[(x,y,z)]);
这篇关于在 ML(SMLNJ) 中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文