COMP 144 Programming Language Concepts
Spring 2004


I/O in ML

In the version of ML we are using, output is slightly different from the class notes, which were written for an older version of ML.

  print("hi there");
  val x = "something";
  print(x) ;

all work to print strings. In fact "print" only works for strings. To make it print other values you have to call conversion functions like this:

  print(Int.toString(43));
  print(Real.toString(3.1415926));
  val m = 125;
  print(Int.toString(m));

Output to a file

As for making the text file, I am not sure redirect like we mentioned in class will work since the interpreter is running interactively. If you can make it for that's ok.

You can easily send output to a text file like this:

  open TextIO;  # put this as the first line of your program

  val outf = openOut("somefyle.txt");

  output(outf,"hi there");
  output(outf,Int.toString(47));

Loading the interpreter

You need not type the while program at the interpreter each time you want to run it. Simple create a text file with your program in it... use whatever editor you like to write the program. Let's say I have my program in a file called prog.ml Then when I run the interpreter I type this:


  use "prog.ml";

The interpreter will slarp in the whole thing, digest it, and spit out any type info and messages that would have been generated had I typed it all in one line at a time.

Hope this all helps.