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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6.1. MANIPULATION<br />

NOTES ON DATA STRUCTURING 117<br />

A mapping which maps all values of its domain <strong>on</strong>to the same value of its<br />

range is known as a c<strong>on</strong>stant mapping. A natural c<strong>on</strong>structor for arrays is<br />

<strong>on</strong>e which takes as argument an arbitrary range value, and yields as result<br />

the c<strong>on</strong>stant array, aU of whose elements are equal to the given range value.<br />

It is c<strong>on</strong>venient to use the type name itself to denote this c<strong>on</strong>structor, e.g.<br />

M = m<strong>on</strong>thtable (31)<br />

is an array such that M[m] = 31 for all m<strong>on</strong>ths m.<br />

cardpack (cardface (heart, King))<br />

is obviously a c<strong>on</strong>juror's pack.<br />

The basic c<strong>on</strong>structive operati<strong>on</strong> <strong>on</strong> an array is that which defines a new<br />

value for <strong>on</strong>e particular element of an array. If x is a value of an array type T,<br />

d a value from its domain type, and r a value from its range type, then we<br />

write:<br />

T(x, d: r)<br />

to denote a value of type T which is identical to x in all respects, except<br />

that it maps the value d into r. The T may be omitted if its existence can be<br />

inferred from c<strong>on</strong>text. Similarly, the c<strong>on</strong>stant array T(x) may be denoted by<br />

all (x).<br />

The basic selecti<strong>on</strong> operator <strong>on</strong> arrays is that of subscripting. This is<br />

effectively a binary operati<strong>on</strong> <strong>on</strong> an array and a value from its domain type;<br />

and it yields the corresp<strong>on</strong>ding value of its range type.<br />

The most comm<strong>on</strong> and efficient way of changing the value of an array is<br />

by selective updating of <strong>on</strong>e of its comp<strong>on</strong>ents, which is accomplished by<br />

the usual notati<strong>on</strong> of placing a subscripted array variable <strong>on</strong> the left of an<br />

assignment:<br />

a[d]: = r.<br />

This means the same as<br />

a: = T(a, d:r).<br />

Note that from an abstract point of view a new value is assigned to the whole<br />

array.<br />

Normally an array type would be regarded as unordered; but in some<br />

cases, particularly character arrays, it is desirable to define an ordering<br />

corresp<strong>on</strong>ding to the normal lexicographic ordering; this is possibte <strong>on</strong>ly<br />

when domain and range types are ordered. In this case the ordering of two<br />

arrays is determined by that of the lowest subscripted elements in which the<br />

two arrays differ. Thus<br />

"BACK" < "BANK"<br />

because the third letter is the first <strong>on</strong>e in which they differ, and

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

Saved successfully!

Ooh no, something went wrong!