II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
NOTES ON DATA STRUCTURING 133<br />
can be represented as the sequence of its coefficients ai. If the degree n of the<br />
polynomial is unpredictable or variable during the course of a calculati<strong>on</strong>,<br />
a sequence is the most appropriate method of defining it:<br />
type polynomial = sequence integer.<br />
Our final example shows how it is possible to represent the programming<br />
language c<strong>on</strong>cept of the identifier. Since in theory an identifier may be of<br />
arbitrary length, a sequence is required. The items of the sequence are either<br />
letters or digits. However, the first character is always alphabetic and may be<br />
separated from the rest. Thus an exact definiti<strong>on</strong> of a data structure corres-<br />
p<strong>on</strong>ding to the identifier is"<br />
type identifier = (first" letter; rest" sequence (l" letter, d" digit)).<br />
8.1 MANIPULATION<br />
The zero element of a sequence type Tis the sequence that c<strong>on</strong>tains no items--<br />
this is known as the null or empty sequence, and is denoted by T(). For<br />
each value v of the domain type, there is a sequence whose <strong>on</strong>ly item is v;<br />
this is known as the unit sequence of v and is denoted by T(v). Finally, if<br />
v l, v2 ..... v n are values from the base type (possibly with repetiti<strong>on</strong>),<br />
T(vl, v2,..., vn) denotes the sequence c<strong>on</strong>sisting of these ~alues in the<br />
stated order. If for c<strong>on</strong>venience the type name T is omitted, we will use<br />
square brackets to surround the sequence:<br />
Iv], [v~,v~ .... , v~]<br />
However, a sequence of characters is normally denoted by enclosing them in<br />
quotes.<br />
The basic operati<strong>on</strong> <strong>on</strong> sequences is c<strong>on</strong>catenati<strong>on</strong>, that is, adjoining two<br />
sequences <strong>on</strong>e after the other. Thus if x is the sequence of characters "PARIS<br />
IN THE" and y is the sequence "THE SPRING", their c<strong>on</strong>catenati<strong>on</strong> x-'y<br />
is the sequence<br />
z = "PARIS IN THETHE SPRING"<br />
Unless the operands are excepti<strong>on</strong>ally small, c<strong>on</strong>catenati<strong>on</strong> is very inefficient<br />
<strong>on</strong> a computer, since it usually involves making fresh copies of both operands.<br />
The programmer should therefore make every effort to replace c<strong>on</strong>catenati<strong>on</strong><br />
by selective updating.<br />
The basic operators for breaking down a sequence into its comp<strong>on</strong>ent parts<br />
are those that yield the first and last items of a n<strong>on</strong>-empty sequence<br />
x. first, x. last<br />
and those that remove the last or first items of a n<strong>on</strong>-empty sequence,<br />
yielding the initial or final segments.<br />
initial (x), final (x).