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.

144 C. A. R. HOARE<br />

Note how we have used sequences to replace the recursi<strong>on</strong> wherever<br />

possible. In fact this can be d<strong>on</strong>e whenever a type name occurs recursively<br />

<strong>on</strong>ly <strong>on</strong>ce at the beginning or at the end of its definiti<strong>on</strong>. For example"<br />

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

might have been formulated recursively:<br />

type expressi<strong>on</strong> =<br />

(empty: (), n<strong>on</strong>-empty: (first: term; final: expressi<strong>on</strong>)).<br />

A similar alternative formulati<strong>on</strong> permits while loops to be expressed as<br />

recursive procedures.<br />

The c<strong>on</strong>structi<strong>on</strong> of values of a recursively defined type requires no new<br />

operators or transfer functi<strong>on</strong>s; all that is needed is recursive use of the<br />

methods defined for the other relevant structuring methods. For example,<br />

the expressi<strong>on</strong><br />

3/(b - 2)<br />

could be specified by the cumbersome c<strong>on</strong>structi<strong>on</strong>"<br />

[term (plus, [factor (times, primary (c<strong>on</strong>st (3))),<br />

])<br />

factor (div, primary (bracketed (<br />

[term (plus, [factor (times, primary (var ("b")))]),<br />

term (minus, [factor (times, primary (c<strong>on</strong>st (2)))])])))<br />

1.<br />

An effective method of getting the computer itself to translate expressi<strong>on</strong>s<br />

into abstract structures will be given as an example in (9.2).<br />

Another familiar example of recursively defined data is the family tree.<br />

A family tree (excluding informati<strong>on</strong> about marriage) can be defined by<br />

associating with each pers<strong>on</strong> the family trees of all his/her offspring. We<br />

assume that certain additi<strong>on</strong>al pers<strong>on</strong>al details are required to be held"<br />

type family = (head:pers<strong>on</strong>; offspring :sequence family);<br />

A pers<strong>on</strong> with no children is an ultimate comp<strong>on</strong>ent of the family tree,<br />

and may be represented:<br />

family (Tom, [])<br />

A family with three children may be represented:<br />

family (Jill, [family (Tom, []),<br />

family (Joanna, []),<br />

family (Matthew, [])]).

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

Saved successfully!

Ooh no, something went wrong!