§ 3.4. Constants literals and built-in primitives
SML# cointains the following atomic types, with the associated literal constants.
| type | its values |
| int | signed integres (of one machine word size) |
| real | double presicion floating point numbers |
| char | characters |
| string | character strings |
| word | unsigned intergers (of one machine word size) |
| bool | boolean values |
We review basis expressions for these atomic types, except for bool which will be treated in the next section.
§ 3.4.1. int type
This is a standard type of integers.
In SML#, a value of int is a two's compliment
integer and is the same as long in C.
Constants of int are written either the usual decimal
notation or hexadecimal notation of the form 0x~.
SML# prints int values in decimal notation.
# 10;
val it = 10 : int
# 0xA;
val it = 10 : int
#~FF;
val it =~255 : int
Binary arithmetic operations +, -, *, div are defined on int.
# 10 + 0xA;
val it = 20 : int
# 0 - 0xA;
val it =~10 : int
# 10 div 2;
val it = 5 : int
# 10 * 2;
val it = 20 : int
As in arithmetic expressions in mathematics, *,div associate stronger than +, -, and operators of the same strength are computed from left to right.
# 1 + 2 * 3;
val it = 7 : int
# 4 - 3 - 2;
val it =~1 : int
§ 3.4.2. real type
real is a type of floating point data.
In SML#, a value of real is a 64 bit double
precision floating point numbers and is the same as double in C.
real constants are written in the notation
# 10.0;
val it = 10.0 : real
# 1E2;
val it = 10.0 : real
# .001;
val it = 0.001 : real
#~1E~2;
val it = 0.001 : real
# 10;
val it = 10 : int
Binary arithmetic operators +, -, *, / are defined for real. Note that the division operator on real is / and not div.
# 10.0 + 1E1;
val it = 20.0 : real
# 0.0 - 0xA;
val it =~10.0 : real
# 10.0 / 2.0;
val it = 5.0 : real
# 10.0 * 2.0;
val it = 20.0 : real
§ 3.4.3. char type
char is a type for one character data.
A constant for a character
\a |
warning (ASCII 7) |
|---|---|
\b |
backspace (ASCII 8) |
\t |
horizontal tab(ASCII 9) |
\n |
new line (ASCII 10) |
\v |
vertical tab (ASCII 11) |
\f |
home feed (ASCII 12) |
\r |
carrige return(ASCII 13) |
\^ |
control character |
\" |
character " |
\\ |
character \ |
\ |
the character whose code is |
# #"a";
val it = #"a" : char
# #"\n";
val it = #"\n" : char
The following primitive functions are defined on char.
val chr : int -> char
val ord : char -> int
chr
# ord #"a";
val it = 97 : int
# chr(97 + 7);
val it = #"h" : char
# ord #"a" - ord #"A";
val it = 32 : int
# chr(ord #"H" + 32);
val it = #"h" : char
§ 3.4.4. string type
string is a type for character strings.
string constants are written with " and ".
A string constant may contain escape sequences shown
above.
For string data, a print function print and a
concatenation binary operator ^ are defined.
# "SML"^"\n"; val it = "SML#\n" : string
# print it;
SML#
val it = () : unit
§ 3.4.5. word type
word is a type for unsigned integers.
word constants are written as 0w
# 0w10;
val it = 0xA : word
# 0wxA;
val it = 0xA : word