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.

NOTES ON DATA STRUCTURING 143<br />

to the same data type as itself; in the same way that a recursive procedure<br />

can call itself recursively from more than <strong>on</strong>e place in its own body. As in<br />

the case of recursive procedures such a structure can c<strong>on</strong>veniently be defined<br />

by writing the name of the type being defined actually inside its own definiti<strong>on</strong>;<br />

or in the case of mutually recursive definiti<strong>on</strong>, in the definiti<strong>on</strong> of some<br />

preceding type.<br />

The most obvious examples of recursive data structures are to be found<br />

in the descripti<strong>on</strong> of arithmetic or logical expressi<strong>on</strong>s, programming lan-<br />

guages, where the recursi<strong>on</strong> reflects the possibility of nesting <strong>on</strong>e expressi<strong>on</strong><br />

inside another. For example, an arithmetic expressi<strong>on</strong> might be defined as<br />

follows:<br />

"An expressi<strong>on</strong> is a series of terms, each of which c<strong>on</strong>sists of a sign<br />

(+ or-) followed by a sequence of factors. Each factor except the first<br />

c<strong>on</strong>sists of a sign (x or/) followed by a primary. A primary is either a<br />

c<strong>on</strong>stant, a variable, or an expressi<strong>on</strong> surrounded by brackets. An initial<br />

plus sign in an expressi<strong>on</strong> may be omitted."<br />

A structured data type whose values comprise such expressi<strong>on</strong>s may be<br />

defined using <strong>on</strong>ly techniques already familiar, plus recursi<strong>on</strong>:<br />

type expressi<strong>on</strong> = sequence term;<br />

type term = (addop: operator; f: sequence factor);<br />

type factor = (mulop: operator; p: primary);<br />

type primary = (c<strong>on</strong>st:(val:real),<br />

var: (id: identifier),<br />

bracketed: (e: expressi<strong>on</strong>));<br />

type operator = (plus, minus, times, div);<br />

This definiti<strong>on</strong> expresses the abstract structure of an arithmetic expressi<strong>on</strong>,<br />

but not the details of its c<strong>on</strong>crete representati<strong>on</strong> as a string of characters.<br />

For example, it does not specify the symbols used for brackets or operators,<br />

nor does it state whether an infix, prefix or postfix notati<strong>on</strong> is used for them.<br />

It does not state how the three kinds of primary are to be distinguished.<br />

It does not even represent the opti<strong>on</strong>al omissi<strong>on</strong> of plus <strong>on</strong> the first term of<br />

an expressi<strong>on</strong>, and the necessary omissi<strong>on</strong> of x <strong>on</strong> the first factor of a term.<br />

Apart from this degree of abstracti<strong>on</strong> and representati<strong>on</strong>-independence, this<br />

type definiti<strong>on</strong> would corresp<strong>on</strong>d to a set of BNF syntax equati<strong>on</strong>s'<br />

(expressi<strong>on</strong>) :: = (term) [ (addop) (term) ]<br />

(term):: = (primary) ] (term)(mulop)(primary)<br />

(expressi<strong>on</strong>) ( addop ) ( term )<br />

(primary) :: = (unsigned real number) I (variable) I<br />

((expressi<strong>on</strong>))

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

Saved successfully!

Ooh no, something went wrong!