Dear All,
I posted a message, asking for help on converting Miranda code
into Haskell, on to the Haskell mailling list as well as the news
group "comp.lang.functional" about two weeks ago. So far, I have
not got any reply yet.
The main question in my message was that
IS THE I/O SYSTEM IN HASKELL ADEQUATE?
>From my experience of programming scientific and engineering computation
problems, most of the programming models (if not all) are as follow:
1) Read source data;
2) Manipulate the source data and generate some global values;
3) There are a number of computation modules which all access
the global values obtained from the source data, and the
results of some computation modules are further used in
other computation modules.
I found that it was very difficult to program such computation models
using Haskell. Either one has to introduce extra arguments for a
computation module (a function) to pass the global values in order
to use them in the module (function), or one has to put the computation
module (function) in a LET expression where the global values being
defined. Both of them are problematic. The first approach introduces
efficiency as well as program clarity penalties. While the second
approach is not feasible for large or even normal size problem.
Maybe I missed something. I would appreciate for your comments
and help.
Thanks in advance.
------------- encl. (my previous message) -------------
Hello,
I have written a program in Miranda (about 2000 lines), and I want to
convert it to Haskell. The program has following features and structure:
1) Read source data from a file;
2) Manipulate the source data and generate some global values;
3) There are a number of computation modules which all access
the global values obtained from the source data, and the
results of some computation modules are further used in
other computation modules.
I list the code of a small Miranda program which captures the same features
as of my original program, except that my original program got more global
values and much more computation:
> ns = map numval (lines (read "data"))
> n = # ns
> sqrts = map sqrt ns
> sum_sqrts = sum sqrts
> avg_sqrts = sum_sqrts / n
> men_sqrts = sum (map minus_avg_sqr sqrts) / n
> where
> minus_avg_sqr x = (x - avg_sqrts) * (x - avg_sqrts)
> result = "number of values = " ++ show n ++ "\n"
> ++ "sum_sqrts = " ++ show sum_sqrts ++ "\n"
> ++ "avg_sqrts = " ++ show avg_sqrts ++ "\n"
> ++ "men_sqrts = " ++ show men_sqrts ++ "\n"
> main = [Stdout result]
The approaches which I could think of were:
1) Add the input stream as an extra argument to every functions
and variables, replace the expression (read "data") by the
argument (i.e. the input stream), and change the main function
accordingly as outlined below:
> main = readChan "data" abort
> (\ s -> appendChan "stdout" (result s) abort done)
> ns s = map read (lines s)
> n s = length (ns s)
... ...
2) Replace the expression (read "data") by the input stream,
change the main function accordingly and put all other function
and variable definitions into a BIG where expression:
> main = readChan "data" abort
> (\ s -> appendChan "stdout" (result s) abort done
> where
> ns = map read (lines s)
> n = length ns
> ... ...
> )
3) Or do something between the above two approaches, but not
really add the input stream as extra argument.
Though the first two approaches are very simple and straightforward, the
approach 1 lose the sharing of global variables completely and the approach
2 can only be applied very very small program. While using the last approach
needs much more thought and may need to change the original code quite lot
depending on the program to be converted.
My questions then are:
How can the Miranda program be easily converted to Haskell?
Will the performance be affected in the resulting Haskell program
in terms of preserving sharing?
Can the approach be applied to large programs?
Thanks in advance for any help.
Please mail me and I'll summarise if there is enough interest.
Liu.
-------
Dr J Liu, Department of Computing, Imperial College,
180 Queens Gate, London SW7 2BZ, United Kingdom.
Telephone : (+44 71/071) 589 5111 ext. 5033 Fax : (+44 71/071) 581 8024
Email: [EMAIL PROTECTED]