Open file in ML(SMLNJ)

不羁岁月 提交于 2019-11-29 18:11:44

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);

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)]);

This problem is perfectly suited to parsing combinators, which you can steal from my friend Greg Morrisett at Harvard.

If you want to understand the underlying ideas, read Graham Hutton's paper Higher-Order Functions for Parsing. If you want to know how to implement I/O in Standard ML, consult the TextIO module in the Standard Basis Library. If you want someone to write the code for you, you may have reached the wrong web site.

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