Functions are central in Standard ML. We’ll learn about functions with a few different examples.
To run this Standard ML program, you would typically save it to a file (e.g., functions.sml) and then use an SML interpreter or compiler. For example, using the Standard ML of New Jersey (SML/NJ) system:
There are several other features to Standard ML functions. One is pattern matching, which we’ll look at next.
Standard ML has some key differences from many other languages:
Function definitions use the fun keyword.
Type annotations are optional but commonly used for clarity.
The last expression in a function is automatically returned.
There’s no explicit main function; top-level expressions are executed in order.
String concatenation uses the ^ operator.
Printing is typically done with the print function, which only handles strings.
These examples demonstrate basic function definitions and calls in Standard ML, showing how to work with multiple parameters and return values.