Signals in Standard ML

Here’s an idiomatic Standard ML example demonstrating a simple “Hello, World!” program:

(* A simple Standard ML program to print "Hello, World!" *)

(* The main function *)
fun main () =
    print "Hello, World!\n"

(* Execute the main function *)
val _ = main ()

Let’s break down this code:

  1. We define a function named main using the fun keyword. This function takes no arguments (indicated by the empty parentheses ()).

  2. Inside the main function, we use the print function to output the string “Hello, World!” followed by a newline character \n.

  3. To execute the main function, we use a value binding val _ = main (). The underscore _ is used as a wildcard pattern, indicating that we don’t care about the result of the function call.

To run this program:

  1. Save the code in a file, for example, hello_world.sml.

  2. Use a Standard ML interpreter or compiler to run the program. For instance, if you’re using Standard ML of New Jersey (SML/NJ):

$ sml hello_world.sml
Hello, World!

Alternatively, you can start the SML/NJ interactive environment and load the file:

$ sml
- use "hello_world.sml";
Hello, World!
val it = () : unit

This example demonstrates the basic structure of a Standard ML program. Some key points to note:

  • Standard ML is a functional programming language, so we define functions rather than using a class-based structure.
  • The language uses fun to define functions and val for value bindings.
  • Standard ML has a strong type system, but types are often inferred, so we don’t need to explicitly declare them in this simple example.
  • The print function is part of the Standard ML Basis Library and is available without any explicit import.

As you explore Standard ML further, you’ll encounter more of its functional programming features and its powerful type system.