ML & Python question
neelk at alum.mit.edu
Sun Dec 31 17:11:04 EST 2000
On Sun, 31 Dec 2000 12:27:06 -0500, Jason Cunliffe <jasonic at nomadicsltd.com>
> Please, Can anyone here provide a intro to what SML is and how
> Python relates to it?
> This is really not intended as Python vs. flame bait question - I am
> interested to expand my progamming horizons and am hoping people
> here might help me to do so.
The very short answer is that Python is a very high level, dynamically
typed OO language, and that standard ML is a very high level,
statically typed functional language. Both are IMO incredibly fun to
The high-level part is straightforward. In both languages, the
language takes care of details that you would have to manage by hand
in lower-level languages. For example, memory is garbage-collected in
Python and SML, and neither language makes you manually track
pointers, array bounds, and the like -- all that is handled
The OO versus functional part is also pretty straightforward. In
functional languages programmers tend to avoid using assignment and
mutable state, whereas in OO languages one uses state, but
encapsulates it inside objects.
The statically typed and dynamically typed part is a bit harder to
explain, and IMO the most fundamental difference between SML and
Python. A couple of examples might be in order. In Python, every value
has a type associated with it:
Whenever you evaluate an expression, Python checks the types at
runtime, and then complains if the types don't work together:
>>> 3 + 5
>>> string.join(["foo", "bar"], '')
>>> 3 + "foo"
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: number coercion failed
In SML you might see something like this,
- 3 + 5;
> val it = 8 : int
- String.concat ["foo", "bar"];
> val it = "foobar" : string
- 3 + "foo";
! Toplevel input:
! 3 + "foo";
! Type clash: expression of type
! cannot have type
This looks almost the same, but there's an important difference: in
Python, the type error is detected at runtime, and in SML, the type
error is detected at compile time, and the compiler quits before ever
trying to evaluate the expression. You simply can't write a type
incorrect SML program.
This is why your friend said that ML is safer -- there is a whole
category of errors that are caught earlier in the programming process.
In languages with very powerful type systems (like SML), very complex
errors can be caught with the type system.
However, the flip side is that Python is a bit more flexible; for
example, the following code in Python works fine:
b = 3 + "foo"
b = "something_odd"
This is a goofy example, but if you need to load and unload complex
objects (such as pieces of code) from and into files and databases,
then dynamic typing makes life much easier.
In short, learning both languages is a good idea. A tutorial in
standard ML (at about the difficulty level of the Python tutorial) can
be found at:
Standard ML has a number of excellent implementations. One of the
best is Standard ML of New Jersey, which can be found at:
Most importantly, have fun. :)
More information about the Python-list