20.03.2013 Views

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

134 c.A.R. HOARE<br />

An important reiati<strong>on</strong>ship between sequences is that <strong>on</strong>e sequence x is<br />

equal to some initial or final subsequence of" a sequence y:<br />

x begins y<br />

or x ends y.<br />

In our previous example, "PARIS" begins z and "RING" ends z. These<br />

two tests can be rather time-c<strong>on</strong>suming in a running program, and should be<br />

avoided wherever possible.<br />

A significant property of sequences is their length, i.e. the number of items<br />

they c<strong>on</strong>tain; this may be found for a sequence x by the functi<strong>on</strong> Iength (x).<br />

For some purposes (e.g. the c<strong>on</strong>structi<strong>on</strong> of a dicti<strong>on</strong>ary) it is useful to<br />

regard a sequence type as ordered in accordance with traditi<strong>on</strong>al lexicographic<br />

principles" as in the case of arrays, the order of" two sequences is determined<br />

by the ordering of the first item in which they differ; or if there is no such<br />

item, a shorter sequence precedes the l<strong>on</strong>ger sequence which it begins, for<br />

example:<br />

"ALPHA"< "ALPHABET".<br />

In this ordering every sequence has a successor, but <strong>on</strong>ly a small proporti<strong>on</strong><br />

have predecessors.<br />

A most important selective updating operati<strong>on</strong> <strong>on</strong> sequences is the<br />

appending of" a new value v to the end of" an existing sequence x. This may be<br />

written:<br />

X" Z()' V<br />

and corresp<strong>on</strong>ds to the familiar c<strong>on</strong>cept of writing a value v to a sequential<br />

file x. The operati<strong>on</strong> corresp<strong>on</strong>ding to reading the beginning of a file x is<br />

<strong>on</strong>e which removes the first item of x and assigns its value to some variable v.<br />

This may be written:<br />

v from x;<br />

In some applicati<strong>on</strong>s, it is useful to be able to read back the most recently<br />

written item from a sequence; this may be expressed<br />

v back from x;<br />

and it removes the last item from x. This operati<strong>on</strong> can be used to "pop up"<br />

the top item of a stack which has been "pushed down" by an ordinary<br />

writing operati<strong>on</strong>"<br />

x' T(v).<br />

If desired, it is possible to define the fourth updating operati<strong>on</strong>, that of<br />

attaching a new value to the beginning of a sequence. (putback (x, v)).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!