CS311 > ML

The ML Programming Language

This offering of CS311 will include laboratory work using the ML programming language. (Previous offerings have used Prolog.)

What is ML ?

ML is best thought as a mult-paradigm language: it supports both functional programming (through a full array of tools such as anonymous first-class functions) and imperative programming (through reference types and "functions" that have effects).

ML is strongly typed, but most of the time there is no need for the programmer to write any type-declarations. This makes ML much more compact and readable than languages that requires explicit type declarations.

ML also has excellent support for Algebraic Data Types (ADTs). ADTs correspond more or less directly to algebras, which are one of the central themes of this course—and that is why ML seems to be the right language to illustrate the course.

Getting Started with ML

There are many implementations of ML. Moscow ML (mosml) is installed on the CS Unix machines; Standard ML of New Jersey (sml) is installed on the CS Unix and CS Linux machines. You will need to use the addpkg script to get access to these packages.

On my personal machine I installed Moscow ML because it was available pre-compiled, so this is the implementation that I have been using for the examples. There are reference documents at the above sites; I've been using:

For Mac users

Thanks to Robert Bermond, I now know that SML is available pre-compiled for the Mac too. You can install a pre-built system using the .dmg installer package in smlnj-ppc-110.65.dmg (or smlnj-x86-110.65.dmg for Intel Macs). For more information, see MACOSXINSTALL.

Stefan Wernli provided the following configuration files and readme for setting up Xcode on Mac OS to get syntax hilighting. You can download the zip file. I edited SML.pbfilespec to allow ml as well as sml as file name extensions:

Extensions = (ml,sml);

If anyone figures-out how to run ML from inside Xcode, please let me know!

The Interactive System

You should probably skim the manual for the interactive system that you plan to use. In case you don't, the following hints may be useful.

Some Sample Code

Here are some short ML function definitions; type them into a file called test.ml.

fun factorial 0 = 1
| factorial n = n * factorial (n-1);

(* the factorial function, defined by giving two cases *)

fun fib 1 = 1
| fib 2 = 1
| fib n = fib(n-1) + fib(n-2);

(* fib n is the nth Fibonacci number *)

fun iota 0 = []
| iota n = 1::(map increment (iota (n-1)))
and increment n = n+1;

(* iota n is just the list [1, 2, 3, ... n]. increment is the function that adds 1 to its argument. map f l is the (built-in) function that applies its first argument f to its second argument (a list), and returns a list of the results. *)

We can then use this in an interactive session; what I typed is in code font.

ruby:~> sml
Standard ML of New Jersey v110.57 [built: Sat Feb 4 22:42:28 2006]
- 4 + 5;
val it = 9 : int
- use "test.ml";
[opening test.ml]
val factorial = fn : int -> int
val fib = fn : int -> int
val iota = fn : int -> int list
val increment = fn : int -> int
val it = () : unit
- fib 4;
val it = 3 : int
- factorial 5;
val it = 120 : int
- iota 6;
val it = [1,2,3,4,5,6] : int list
- map fib (iota 6);
val it = [1,1,2,3,5,8] : int list
- map factorial (iota 10);
val it = [1,2,6,24,120,720,5040,40320,362880,3628800] : int list
-

When you read in code, notice that the system responds with the types of the functions that you have defined. It's a good idea to make sure that these are what you expect! Look at the transcript of the session above, and make sure that you understand why the system prints what it does. Better yet, try some other expressions of your own choice.


Most recently modified on Monday 1 October 2007 at 15.11


Andrew P. Black