Constants in Standard ML
On this page
Constants in Standard ML (SML) are handled differently than in some other languages. SML is a statically-typed, functional language, and creating constants involves defining values which are immutable by default.
Here is how you can declare constants and use them in SML:
Go supports constants of character, string, boolean, and numeric values.
Within a function, constants can be defined similarly and are immutable.
Explanation
String Constant:
This statement declares a string constant
s
with the value “constant”.Main Function Equivalent:
The
let
block is used to declare local constants and perform operations. Theval _ =
is a common idiom in SML to execute a block of code at startup.Numeric Constants:
Here
n
is an integer constant, andd
is a real number obtained by performing a division with high precision.Printing Values:
These lines print the values of the constants
s
,d
and some more complex expressions involving these constants. SML usesprint
for output,^
for string concatenation, and functions from theReal
andMath
structures for numeric operations.
To run the above SML code, you can use an SML interpreter such as Moscow ML or SML/NJ. Save the code to a file, for example, constants.sml
, and then run:
By this, we have utilized Standard ML to declare constants and perform arithmetic with arbitrary precision, similar to the provided example.