We shall be using Standard ML of New Jersey in our course work. It is one of the best-known and most widely-used Standard ML systems.
The interpretive mode is great for testing out functions one by one, but
it's not effective for typing in lots of functions or making changes
to them.
SML provides you with a function for reading in an SML program from a
file. The function is use (its type is string -> unit).
Suppose that you had typed in the following SML code into a file called
hello.sml :
fun hello () = print ("Hello, world\n" );
Now, we fire up SML and read in this file :
% sml
Standard ML of New Jersey, Version 110.0.3, January 30, 1998
- use "hello.sml";
[opening hello.sml]
val hello = fn : unit -> unit
val it = () : unit
- hello;
val it = fn : unit -> unit
- hello ();
Hello, world
val it = () : unit
-
During an interactive session with SML,
you may have definitions in the current environment that you
want to save for future sessions. SML provides you with a function
to save the current environment : SMLofNJ.exportML
(whose type is string -> bool). Entering
SMLofNJ.exportML("saved_env");
will create a file called saved_env which is basically a
dump of the entire memory contents
(including the code for the interpreter and
the environment). Later you can restart SML, naming the file
on the shell command line to resume execution in the same environment
in which the exportML was done.
That is, you'd enter
% sml saved_env
to restart SML.
Beware -- the saved file can be HUGE (at least several
megabytes).
There is no debugger currently installed with SML/NJ.
(A special version of the SML interpreter that can then interact with GNU
Emacs exists). SML is a strongly typed language and its type inference
system is powerful enough to capture a wide variety of errors and
ambiguities. Furthermore,
there is run-time checking so there's no way you can crash the system
(except for an infinite loop). SML does provide you with references which
are pointers (and hence dangerous), but their use is deprecated as not
in keeping with the functional style.
A convenient way to develop ML programs is to open two windows, one for an editor and one to run sml. You edit ML functions in the edit window, using your favorite text editor. To test ML functions, go to the sml window. Enter the command use "filename.sml"; to load and compile the file you've just edited. You can interactively test each function as you add it. When changes, corrections, or extensions are needed, you return to the edit window.
A good approach is to write and test functions one by one. That way you can focus on a limited amount of code. If necessary, print statements can be added to view selected values during execution.