Kristian Lunde

Hello Standard ML (of New Jersey)

with 4 comments

Back when I was at the university we had a course called “Programming languages”, there we learned a little something about a lot of known and less known programming languages. One of these languages was Standard ML, and I remember that I was quite fascinated with that language, it was so different from everything else we had learned. Since then the functional languages have become a bit more common, with Erlang, F#, Clojure, Scala and Haskell as some examples (more functional languages can be found here: I have not used any functional languages since then and I thought I would just briefly refresh my memory by installing Standard ML and try the most basics of the language.


I am currently using a Mac so installing sml-nj was really simple. I downloaded the mac .dmg package from and ran the installer. the system was installed by default at /usr/local/smlnj-110.68. I added the path to my .profile file, which enables me to start sml without being in the sml bin root folder.

When that was done I could start the sml shell (just type sml in your shell):

  1. kristian-lundes-macbook-pro:sml kristianlunde$ sml
  2. Standard ML of New Jersey v110.68 [built: Thu Sep  4 16:23:20 2008]

Example 1: Start standard ML

(To exit the sml shell use key combination: CTRL + D).

First touch – Hello World

I always do a Hello World programming in a new language. Even though I have been playing around with Standard ML before I do not remember a hole lot of it, so I start from scratch.

  1. - "Hello World";
  2. val it = "Hello World" : string

Example 2: Print “Hello World”

The – is the sml prompt, to end a expression the semi-colon is used. So to write Hello World all you have to do is to apply quotes around the text and end it with ; and hit enter. The result gives us the text and the data type.

The first function

To do a Hello world proved to be quite simple, so lets take it a bit further and write a function which prints the hello world text.

  1. 1. – fun hello ():string = "Hello World";
  2. 2. val hello = fn : unit -> string
  3. 3. – hello();
  4. 4. val it = "Hello World" : string

Example 3: The first function

The function itself is a one liner seen on line 1 in the example above. The function is created using the fun keyword, the function is named hello and have no parameters which result in the (). The function return a result of the data type string, this is defined with the :string element, The content of the function start after the equal sign, and is just a print of the hello world text. To end the function close semi-colon.

When creating a function which is compiled we get the response seen in line 2. This tell us that hello is a function and that it return a string. I believe the unit text mean that the function does not take any parameters (I might be wrong).

To execute the function just type the name of the function, see line 3 in the example above, this result in printing out the “hello world” text as seen in line 4. Line 4 tell us that the result of the function is “Hello World” and the data type is a string.

Functions with parameters

Now we have created our first function, but a function usually need one or more parameters, so let us have a look at that. The next function is a simple echo function which just print the text to screen.

  1. 1. – fun echo (s:string):string = s;
  2. 2. val echo = fn : string -> string
  3. 3. – echo("Hello World");
  4. 4. val it = "Hello World" : string

Example 4: Function with parameter

Line 1 in example 4 define the function, it takes one parameter s as a string, the return data type is also a string and it just output the string to the shell. When the function is executed in line 3 the parameter is “Hello World” and the result is seen in line 4.

To add more than one parameter just add a comma between the parameters. Remember to define a data type on all the parameters.

Example 5 is a function which takes to parameters and sum the two parameters together before returning the result.

  1. 1. – fun sum(x:int,y:int):int = x + y;
  2. 2. val sum = fn : int * int -> int
  3. 3. – sum(10,20);
  4. 4. val it = 30 : int

Example 5: function with multiple parameters

Lambda expressions

Lambda expressions is also known as anonymous functions, and create a function without any name. This is used quite frequently in sml and are written as seen in example 6.

  1. fn() => print "Hello World";

Example 6: lambda expression.

The example 6 do not do much except print out the Hello world text when it is called. Naturally a lambda expression must be called as part of another function or code snippet.

Load a function from file

To load sml code from a file just use:

  1. - use "../sml/myfile.sml";

Where you replace ../sml/myfile.sml to point to your file.


This is the best online introductions I have found so far are:

A short FAQ:

I really have not showed anything of the fancy stuff you could go about and build with Standard ML, but hopefully I am able to spend some more hours playing around with standard ML and write another blog post.

Be Sociable, Share!

Written by Kristian Lunde

December 17th, 2008 at 12:52 am

Posted in Misc,Programming

Tagged with ,

4 Responses to 'Hello Standard ML (of New Jersey)'

Subscribe to comments with RSS or TrackBack to 'Hello Standard ML (of New Jersey)'.

  1. I tried installing the dmg for osx and when I ran it

    I got these message

    /usr/local/smlnj-110.71/bin/.run-sml: Fatal error — unable to open heap image “/usr/local/smlnj-110.71/bin/.heap/.run-sml”

    I have tried a few others versions and cannot get them to run at all :(


    4 Feb 10 at 06:25:27

  2. Sorry mate, I have not tried to install the latest version of sml on my mac. I’ll see if I can get round to installe it soon.

    Kristian Lunde

    4 Feb 10 at 11:28:21

  3. [...] Standard ML of New Jersey provides an interactive compiler for Windows, Linux, amongst others. …Hello Standard ML (of New Jersey) | Kristian LundeBack when I was at the university we had a course called Programming languages, there we learned a [...]

  4. Quoting whichet
    Make it simple, add path to my .profile file.
    It seems the sml exec doesn’t like run command.

    1. Open ~/.bash_profile in text editor
    .bash_profile is hidden file by default
    2. Add line: export PATH=$PATH:/usr/local/smlnj-110.72/bin/
    3. Make sure the version number much yours
    4. Save .bash_profile
    5. Relaunch Terminal
    6. Type now: sml

    Works fine Mac OS 10.6.


    12 Dec 10 at 20:02:21

Leave a Reply

Get Adobe Flash player