SML# Document Version 4.1.0
7 Introduction to ML programming

7.4 Constants literals and built-in primitives

SML# contains the following atomic types, with the associated literal constants.

type its values
int signed integers (of one machine word size)
real double precision floating point numbers
char characters
string character strings
word unsigned integers (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.

7.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 0xh where h is a sequence of digits from 0 to 9 and letters from a(or A) to f(or F). A negative constant is written with ~. SML# prints int values in decimal notation.

# 10;
val it = 10 : int
# 0xA;
val it = 10 : int
# ~0xFF;
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

7.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 s.dEs, where s is a decimal string possibly with the negation symbol and d is a decimal string. Each of the three parts can be omitted, but decimal string is interpreted as a value of int.

# 10.0;
val it = 10.0 : real
# 1E2;
val it = 100.0 : real
# 0.001;
val it = 0.001 : real
# ~1E~2;
val it = ~0.01 : 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 - 10.0;
val it = ~10.0 : real
# 10.0 / 2.0;
val it = 5.0 : real
# 10.0 * 2.0;
val it = 20.0 : real

7.4.3 char type

char is a type for one character data. A constant for a character c is written as #"c". c may be any printable character or one of the following special escape sequences.

\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 carriage return(ASCII 13)
\^C control character C
\" character "
\\ character \
\ddd the character whose code is ddd in decimal

# #"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 n assumes that n is an ASCII code and returns the character of that code. ord c returns the ASCII code of c.

# 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

7.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

7.4.5 word type

word is a type for unsigned integers. word constants are written as 0wd or 0wxh where d is a decimal digit sequence and h is a hexadecimal digit sequence. SML# prints word data in a hexadecimal notation.

# 0w10;
val it = 0wxa : word
# 0wxA;
val it = 0wxa : word