Getting Started with StandardML FYI

Home Documentation

Getting Started with Standard ML

Welcome to StandardML FYI! This guide will help you get started with Standard ML programming, from installation to writing your first programs.

What is Standard ML?

Standard ML (SML) is a general-purpose, modular, functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of theorem provers.

Key Features

  • Strong Static Typing: Catches many errors at compile time
  • Type Inference: The compiler can infer most types automatically
  • Pattern Matching: Powerful way to destructure data and control flow
  • Functional: First-class functions and immutable data structures
  • Modular: Advanced module system for large program organization
  • Garbage Collection: Automatic memory management

Installation

SML/NJ (Standard ML of New Jersey)

The most widely used implementation.

macOS with Homebrew:

brew install smlnj

Manual Installation:

  1. Download from smlnj.org
  2. Follow installation instructions for your platform

MLton

A whole-program optimizing compiler.

macOS with Homebrew:

brew install mlton

Poly/ML

Another popular implementation.

macOS with Homebrew:

brew install polyml

Verifying Installation

Start the SML/NJ interactive environment:

sml

You should see:

Standard ML of New Jersey v110.99
-

Your First Standard ML Program

Hello World

Create a file called hello.sml:

print "Hello, StandardML FYI!\n";

Run it:

sml hello.sml

Using the REPL

The SML interactive environment is excellent for learning:

- val greeting = "Hello, World!";
val greeting = "Hello, World!" : string

- fun square x = x * x;
val square = fn : int -> int

- square 5;
val it = 25 : int

- val numbers = [1, 2, 3, 4, 5];
val numbers = [1,2,3,4,5] : int list

- map square numbers;
val it = [1,4,9,16,25] : int list

Basic Concepts

Values and Variables

(* Immutable values *)
val pi = 3.14159;
val name = "StandardML";

(* Type annotations (optional due to inference) *)
val count : int = 42;
val temperature : real = 98.6;

Functions

(* Simple function *)
fun double x = x + x;

(* Function with type annotation *)
fun multiply (x : int, y : int) : int = x * y;

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

Pattern Matching

(* Pattern matching on lists *)
fun sum [] = 0
  | sum (x::xs) = x + sum xs;

(* Pattern matching on custom types *)
datatype shape = Circle of real | Rectangle of real * real;

fun area (Circle r) = 3.14159 * r * r
  | area (Rectangle (w, h)) = w * h;

Lists and Tuples

(* Lists *)
val fruits = ["apple", "banana", "cherry"];
val numbers = [1, 2, 3, 4, 5];

(* Tuples *)
val point = (3.0, 4.0);
val person = ("Alice", 30, true);

(* List operations *)
val first = hd numbers;        (* head *)
val rest = tl numbers;         (* tail *)
val length = List.length fruits;

Next Steps

Now that you have Standard ML set up, explore these topics:

  1. Data Types - Custom types and pattern matching
  2. Functions - Higher-order functions and recursion
  3. Modules - Organizing code with structures and functors
  4. Type System - Understanding ML’s powerful type system
  5. Standard Basis Library - Built-in functions and modules

Resources

Ready to dive deeper? Check out our comprehensive documentation for detailed guides and examples.