[Haskell-cafe] Annotating calculations

Henning Thielemann lemming at henning-thielemann.de
Wed Jun 15 11:59:34 EDT 2005

On Wed, 15 Jun 2005, Rene de Visser wrote:

> Hello,
> I have a somewhat complicated calculation programmed in Haskell.
> This calculation is coded without using monads.
> I want to also produce a report describing the details of this calculation
> for each particular set of inputs.
> e.g. Number of hours worked = 100. Gross pay per hour = 50. Total gross =
> 100 * 50 = 500.

Is this example computed with Excel? :-]

> I think if I add the report generating functions into the calculation
> functions, it will make them twice as messy, and they are already
> complicated enough.
> On the other hand replicating the calculation source code twice, once
> without reporting and once without seems bad.

I think that it is good style to separate computations from input/output
operations and formatting. I suggest to divide the calculatoin into
smaller parts. If you have more breaks you have more chances to get
temporary results. You can put temporary values into a data structure.
E.g. if you have an iteration don't write a recursion with a fixed abort
criterion but write a function which maps the old value to the new one,
then apply 'iterate' on it. Now you can inspect the temporary values and
you can later apply a function which decides when to stop the iteration.

More information about the Haskell-Cafe mailing list