home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / function / 927 < prev    next >
Encoding:
Internet Message Format  |  1992-07-21  |  3.4 KB

  1. Path: sparky!uunet!mcsun!uknet!glasgow!daemon
  2. From: jjl@doc.ic.ac.uk (Junxian J Liu)
  3. Newsgroups: comp.lang.functional
  4. Subject: Questions on converting Miranda code to Haskell
  5. Message-ID: <13833.9207201557@peaberry.doc.ic.ac.uk>
  6. Date: 20 Jul 92 15:57:57 GMT
  7. Sender: daemon@dcs.glasgow.ac.uk
  8. Organization: Glasgow University Computing Science Dept.
  9. Lines: 94
  10. Approved: usenet@dcs.glasgow.ac.uk
  11. X-Mailer: mail-news 2.0.5
  12.  
  13. Hello,
  14.  
  15. I have written a program in Miranda (about 2000 lines), and I want to
  16. convert it to Haskell. The program has following features and structure:
  17.  
  18.            1) Read source data from a file;
  19.     2) Manipulate the source data and generate some global values;
  20.     3) There are a number of computation modules which all access
  21.        the global values obtained from the source data, and the 
  22.        results of some computation modules are further used in
  23.        other computation modules.
  24.     
  25. I list the code of a small Miranda program which captures the same features 
  26. as of my original program, except that my original program got more global
  27. values and much more computation:
  28.      
  29. | ns    = map numval (lines (read "data"))
  30.  
  31. | n     = # ns
  32.  
  33. | sqrts = map sqrt ns
  34.  
  35. | sum_sqrts = sum sqrts
  36.  
  37. | avg_sqrts = sum_sqrts / n
  38.  
  39. | men_sqrts = sum (map minus_avg_sqr sqrts) / n
  40. |             where
  41. |             minus_avg_sqr x = (x - avg_sqrts) * (x - avg_sqrts)
  42.  
  43. | result  = "number of values = " ++ show n ++ "\n"
  44. |           ++ "sum_sqrts = " ++ show sum_sqrts ++ "\n"
  45. |           ++ "avg_sqrts = " ++ show avg_sqrts ++ "\n"
  46. |           ++ "men_sqrts = " ++ show men_sqrts ++ "\n"
  47.  
  48. | main    = [Stdout result]
  49.  
  50.  
  51. The approaches which I could think of were:
  52.  
  53.     1) Add the input stream as an extra argument to every functions 
  54.        and variables, replace the expression (read "data") by the 
  55.        argument (i.e. the input stream), and change the main function
  56.        accordingly as outlined below:
  57.  
  58.         > main = readChan "data" abort 
  59.         >     (\ s -> appendChan "stdout" (result s) abort done)
  60.         
  61.         > ns s = map read (lines s)
  62.         
  63.         > n  s = length (ns s)
  64.         ... ...
  65.  
  66.     2) Replace the expression (read "data") by the input stream, 
  67.        change the main function accordingly and put all other function
  68.        and variable definitions into a BIG where expression:
  69.  
  70.         > main = readChan "data" abort
  71.         >        (\ s -> appendChan "stdout" (result s) abort done
  72.         >         where
  73.         >         ns = map read (lines s)
  74.         >         n  = length ns
  75.         >         ... ...
  76.         >     )
  77.  
  78.     3) Or do something between the above two approaches, but not
  79.        really add the input stream as extra argument.
  80.  
  81. Though the first two approaches are very simple and straightforward, the
  82. approach 1 lose the sharing of global variables completely and the approach
  83. 2 can only be applied very very small program. While using the last approach
  84. needs much more thought and may need to change the original code quite lot 
  85. depending on the program to be converted.
  86.  
  87.  
  88. My questions then are: 
  89.  
  90.     How can the Miranda program be easily converted to Haskell?
  91.  
  92.     Will the performance be affected in the resulting Haskell program
  93.     in terms of preserving sharing?
  94.  
  95.     Can the approach be applied to large programs?
  96.  
  97. Thanks in advance for any help.
  98.  
  99. Please mail me and I'll summarise if there is enough interest.
  100.  
  101. Liu.
  102. -------
  103. Dr J Liu,   Department of Computing, Imperial College, 
  104.             180 Queens Gate, London SW7 2BZ, United Kingdom.
  105. Telephone : (+44 71/071) 589 5111 ext. 5033  Fax : (+44 71/071) 581 8024
  106. Email:      jjl@doc.ic.ac.uk
  107.