SML has been installed on all Sun workstations (i.e., flat-rock, hendersonville, marshall, and brevard).
To get you started here are a few basics on running SML:
user.sml
type:
use ("myfile.sml");
The primary URL is http://cm.bell-labs.com/cm/cs/what/smlnj/index.html. This site contains links to open source implementations of SML as well as many other resources including tutorials.
Two other excellent SML tutorials are:
Two files are being provided to help you get started: max.sml and integers.txt. The first file is an ML program, the second is the input file.
Once you have the downloaded the files, use SML to run the max program and make sure things are working properly. In the directory where you have stored the files, startup SML, and you should see something like:
Standard ML of New Jersey, Version 110.0.3, January 30, 1999
[CM&CMB]
-
Now load the max program the command:
- use("max.sml");
Note that SML is case-sensitive. If something goes wrong, the general
rule is to type CTRL-C and try again. SML will then compile the
program and display the definition of each function:
[opening max.sml]
val digit = fn : char -> int
val skipEOL = fn : char list -> char list
val stripEOL = fn : string -> string
val chars2int = fn : char list * int -> int
val str2int = fn : string -> int
val readInt = fn : TextIO.instream -> int
val writeInt = fn : int -> int
val max = fn : int * int -> int
val maxInFile = fn : TextIO.instream -> int
val main = fn : unit -> int
val it = () : unit
-
To run the program, call the main function and see what happens:
- main();
1421
val it = 1421 : int
-
The program output 1421 as the maximum integer in the file. To exit
SML, simply type CTRL-D.
To change the program, use an editor outside of SML, and then reload the program into SML via use(...). Note that SML compiles the program from top to bottom, and generally stops on the first compiler error. Error messages are not very good, but should contain the line # of the offending line.
Your assignment is to write a program that computes the integer average of positive integers stored in a file. The file is called, "integers.txt", contains one integer per line, and may be empty. Copy the provided "max.sml" to a file called "avg.sml" and start from there; in particular, "max.sml" contains the helpful I/O functions readInt and writeInt for your use. The only requirement is that your solution must read the input file exactly once. In other words, the following style of solution is not legal:
fun main() =
writeInt(sum("integers.txt") div count("integers.txt"));
This would process the input file twice. Instead, your main function
should look something like:
fun main() =
writeInt(avg(TextIO.openIn("integers.txt")));
This passes an open file stream to avg, which is now restricted to
reading through the file once. Note that even though you can't read
the input file more than once, you are free to use lists in your
solution (which you can traverse as many times as you want). Ignoring
the I/O routines, expect the program to require about 10-15 lines of ML
code.
Hand in your program by submitting it to your ftp directory for this class.