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.

NOTES ON DATA STRUCTURING 135<br />

In some cases, it is more efficient to avoid the copying of an item which is<br />

involved in the from operati<strong>on</strong>. These cases may be dealt with by merely<br />

omitting the left hand variable, e.g.<br />

from x<br />

back from x.<br />

In this case, access to the items of the sequence will usually be made by the<br />

selectors x. first and/or x. last.<br />

It is very comm<strong>on</strong> to wish to scan all the items of a sequence in successi<strong>on</strong>;<br />

a suitable notati<strong>on</strong> for this is modelled <strong>on</strong> the for statement:<br />

for v in x do S;<br />

If x is empty, the statement is omitted. Otherwise the variable v (regarded<br />

as local to S) takes in successi<strong>on</strong> the values of all items from the sequence<br />

x, and S is executed <strong>on</strong>ce for each value. In this c<strong>on</strong>structi<strong>on</strong> neither x nor v<br />

should be updated within S.<br />

A similar c<strong>on</strong>structi<strong>on</strong> can be used for defining a sequence as an item-by-<br />

item transformati<strong>on</strong> E(v) of items v in sequence s.<br />

for v in s take E(v).<br />

In deciding a representati<strong>on</strong> for a sequence, it is most important to know<br />

which of the selective updating operati<strong>on</strong>s are going to be carried out up<strong>on</strong> it.<br />

(1) If the <strong>on</strong>ly operati<strong>on</strong> is from, the sequence is known as an input<br />

sequence; obviously in order to have any value at all, an input sequence<br />

must be initialised to some value existing in the outer envir<strong>on</strong>ment in which<br />

it is declared. The associati<strong>on</strong> of a sequence local to a program with some<br />

file existing more or less permanently <strong>on</strong> backing store is often known as<br />

"opening" the file for input, and we assume that this operati<strong>on</strong> is invoked<br />

implicitly <strong>on</strong> declarati<strong>on</strong> of a local input sequence. The reverse operati<strong>on</strong> of<br />

"closing" the file is invoked implicitly <strong>on</strong> exit from the block to which<br />

the sequence is local.<br />

(2) If the <strong>on</strong>ly operati<strong>on</strong> is writing to the file, the sequence is known as an<br />

output sequence. An output sequence may be initialised from the envir<strong>on</strong>ment<br />

in the same way as an input sequence; or more comm<strong>on</strong>ly, it may take an<br />

empty initial value. In either case, in order to serve any useful purpose, the<br />

final value of the sequence <strong>on</strong> exit from the block must be assigned to some<br />

variable existing in the outer envir<strong>on</strong>ment in which the sequence is declared.<br />

The identity of this outer variable should be declared together with the<br />

sequence; if this outer variable is held more or less permanently <strong>on</strong> backing<br />

store, it is known as an output file; and the rules for implicit invocati<strong>on</strong> of<br />

opening and closing of the file <strong>on</strong> entry and exit to the block are similar to<br />

those for input files.

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

Saved successfully!

Ooh no, something went wrong!