24.05.2014 Views

pdf: 600KB - Potsdam Institute for Climate Impact Research

pdf: 600KB - Potsdam Institute for Climate Impact Research

pdf: 600KB - Potsdam Institute for Climate Impact Research

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.

19<br />

and write a generic program which is able to interpret the data in the file and produce the<br />

appropriate output <strong>for</strong> the printer. The specialised, one-off program that executes print and draw<br />

instructions is replaced by a more generic one, plus a data file containing some in<strong>for</strong>mation.<br />

We can venture the following principle:<br />

Any given procedural computer program can be replaced by a declarative bit (data)<br />

and a more generic procedural program.<br />

Understanding this concept is absolutely fundamental to appreciating the concept of declarative<br />

modelling. This also involves shifting in<strong>for</strong>mation (the model specification) from a procedural<br />

program (the way models are currently implemented) to data in a data file, to be processed by a<br />

more generic program.<br />

5.3 Two examples of shifting the declarative:procedural boundary<br />

We will now explore these concepts in a bit more detail, by considering 2 examples of moving the<br />

declarative/procedural boundary:<br />

1 - a simple data-processing program;<br />

2 - a simple function-evaluation program.<br />

We will then consider why one should want to do this: what are the benefits of increasing the<br />

declarative content of a solution to a problem?<br />

A simple data-processing program.<br />

You'd probably be pretty horrified if you asked someone to work out the total of some numbers,<br />

and they came up with the following program:<br />

a(1)=27.1; a(2)=53.2; a(3)=41.9...... a(157)=19.2<br />

sum=0; <strong>for</strong> i = 1 to 157; sum = sum+a(i); next i<br />

print sum<br />

Why? Because this one program contains both the data as program assignment statements, and the<br />

instructions <strong>for</strong> analysing them. Instead, you would expect the data to be in one file (data1), and a<br />

more generic version of the program in another (prog1).<br />

File data1<br />

157<br />

27.1, 53.2, 41.9, .... 19.2<br />

File prog1<br />

input n<br />

<strong>for</strong> i = 1 to n; input a(i); next i<br />

sum = 0; <strong>for</strong> i = 1 to n; sum = sum+ a(i); next i<br />

print sum<br />

Separating data and program gives huge benefits: the same data can now be analysed by different<br />

programs; and the same program can be applied to different data sets.<br />

A simple function-evaluation program<br />

Consider the following program, to work out the diameter of a circle from its radius:<br />

input radius<br />

diameter = 6.28*radius<br />

print "radius="; radius; " diameter="; diameter<br />

Here, the data (well, datum: the value <strong>for</strong> radius) has already been separated out (as one would<br />

expect, in the light of the previous discussion).

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

Saved successfully!

Ooh no, something went wrong!