Signals in Standard ML
Here’s an idiomatic Standard ML example demonstrating a simple “Hello, World!” program:
Let’s break down this code:
We define a function named
main
using thefun
keyword. This function takes no arguments (indicated by the empty parentheses()
).Inside the
main
function, we use theprint
function to output the string “Hello, World!” followed by a newline character\n
.To execute the
main
function, 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):
Alternatively, you can start the SML/NJ interactive environment and load the file:
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 andval
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.