B.7 Tuples

A tuple is a sequence of elements of potentially mixed types. Formally, a tuple is an element e of a Cartesian product of a given number of sets: e ∈ (S1 × S2 × … × Sn). A two-element tuple is called a pair [e.g., e ∈ (A × B)]. A three-element tuple is called a triple [e.g., e ∈ (A × B × C)]. A tuple typically contains unordered, heterogeneous elements akin to a struct in C with the exception that a tuple is indexed by numbers (like a list) rather than by field names (like a struct). While tuples can be heterogeneous, in a list of tuples, each tuple in the list must be of the same type. Elements of a tuple are accessible by prefacing the tuple with #n, where n is the number of the element, starting with 1:

A set of four code lines in M L with a tuple.
Description

The response from the interpreter when (1, "Mary", 3.76) (line 1) is entered is (1, "Mary", 3.76) : int * string * real (line 2). This response indicates that the tuple (1, "Mary", 3.76) consists of an instance of type int, an instance of type string, and an instance of type real. The response from the interpreter when a tuple is entered (e.g., int * string * real)demonstrates that a tuple is an element of a Cartesian product of a given number of sets. Here, the *, which is not intended to mean multiplication, is the analog of the Cartesian-product operator ×, and the data types are the sets involved in the Cartesian product. In other words, int * string * real is a type defined by the Cartesian product of the set of all ints, the set of all strings, and the set of all reals. An element of the Cartesian product of the set of all ints, the set of all strings, and the set of all realshas thetype int * string * real:

A code line in M L.
Description

The argument list of a function in ML, described in Section B.8, is a tuple. Therefore, ML uses tuples to specify the domain of a function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.7.102