Type Ctrl-D to exit the ML interpreter if you run it interactively.
Comments in ML have the form (* ... *)
.
By default, ML abbreviates large lists, data structures, and strings
when printing them. For example, it may print \"[1,2,3,4,....]
\"
instead of the complete contents of a list, or
\"Node(Node(Node(#,#),#),Leaf(3))
\" where the #
's represent
unprinted parts of a datatype value. Include the following line at the
top of the file to print larger datatype values before resorting to
#
.
Control.Print.printDepth := 100;
Similarly, include the following lines to print long lists and strings:
Control.Print.printLength := 500;
Control.Print.stringDepth := 500;
ML reports a warning
whenever you use =
to compare two values with polymorphic type. For
example,
fun eq(x,y) = (x = y);
will cause this warning to be generated, because x
and y
will have
polymorphic type ''a
. This is perfectly fine and you may ignore the
warning. It is not reporting any kind of semantic error or type error in
your code.
The compiler reports the warning because testing whether two values
are equal. For example, imagine that x
and y
are lists. It could
take a looong time to check if they are equal if they contain many elements.
You may only use a name once in any pattern. Thus,
fun eq(x,x) = true
| eq(x,y) = false;
will give you an error. You would need to rewrite the above as we wrote it in the last section:
fun eq(x,y) = (x = y);
You are performing pattern matching, but not handling all cases. In other words, some value exists that does not match any of the patterns you provide. If pattern matching is applied to such a value, the program will fail at run time with an exception.
If you are pattern matching on lists, be sure to handle the nil and non-empty cases. For datatypes, be sure to handle each different constrctor form with its own case.
If you are getting syntax errors that you cannot decipher, double check the following:
Be sure to have ;
in the right places --- it is a separator, not a
terminator:
sml
fun f(x) =
let val z = 3;
val y = 2
in
print z;
print y
end;
Be sure that datatype constructors occuring in patterns are surrounded by parenthesis:
sml
fun maptree (LEAF(x)) = ... (* OK *)
| ...
instead of
sml
fun maptree LEAF(x) = ... (* BAD *)
| ...
If all else fails, you may find it useful to consult the complete list of SML compiler messages and what they mean.