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:
We define a function named
mainusing thefunkeyword. This function takes no arguments (indicated by the empty parentheses()).Inside the
mainfunction, we use theprintfunction to output the string “Hello, World!” followed by a newline character\n.To execute the
mainfunction, we use a value bindingval _ = 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:
Save the code in a file, for example,
hello_world.sml.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 = () : unitThis 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
funto define functions andvalfor 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
printfunction 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.