English 中文(简体)
Open file in ML(SMLNJ)
原标题:

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

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.





相关问题
Standard ml function in datatype problem

I have to create a function about peano numbers defined as the following datatype: datatype a peano = P of ( a -> a) * a -> a val zero = P(fn (f, x) => x) The function that I have to ...

convert string to list in Standard ML [duplicate]

Possible Duplicate: Open file in ML(SMLNJ) I have a string value which has value like this: "[(1,2,3),(2,3),(6,8)]" -> string but I want to have these values in int type like this: [(1,2,3),...

Open file in ML(SMLNJ)

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 ),( ...

Line Comments in Standard ML

I m learning ML, with the SML/NJ dialect. What I m trying to figure out is if there is a line comment operator. I found the block comment operator, (* ... *), but I really miss line comments. ...

How to fix the SML/NJ interactive system to use Arrow Keys

I m having some trouble using SML/NJ interactive system, namely, that when I try to use my arrow keys (either left or right to make a correction in the expression I ve typed, up to repeat the last ...

What causes this Standard-ML type error?

I was trying to make a tail-recursive version of this very simple SML function: fun suffixes [] = [[]] | suffixes (x::xs) = (x::xs) :: suffixes xs; During the course of this, I was using type ...

热门标签