
Hi,
I have been playing around with Haskell for about a month now and reading the nice book "Real World Haskell." My main reason for learning Haskell is that I want to code up some machine learning projects (heavy use of matricies). Normally, I use Mathematica and mix it with C++, but Mathematica is proprietary, slow, and can't produce executable while C++ is verbose. Learning Haskell has been fun; however, I have been a little worried that I will sacrifice too much performance when coding in Haskell.
So, I recently asked one of my friends the following question, "Say you had a C program. Can you always convert it to Haskell in such a way that the compiled Haskell is not too slow and does not need too much memory?" Supposing that too slow means slower than 1/4 the speed of C and too much memory means twice the memory of C. Hopefully, someone that knows Haskell well can comment on this question.
I am not sure, but I think the answer is yes, such a conversion can always be done and creating a C to Haskell compiler with the above performance constraints is not extremely hard. I started thinking about how a compiler might convert a simple C program into Haskell. Below I will paste a C program and the compile-by-hand Haskell code. It seems to me that the ideas I used to create the Haskell code can be implemented in a compiler that converts a simple subset of C into Haskell. I was thinking about restricting the C to one data type 32bit-integers, arithmetic (+-*/%), assignment (=), comparison (<,>,==,<=,>=), the if-condition-codeblock construct, and the while-condition-codeblock construct. (I would also like to do integer arrays, but I have not read about mutable arrays and monads yet.)
Any comments?
Cheers,
Hein H.
--------------------C to Haskell------------------------
module Main where
-- Convert a C-Program line by line into Haskell
--
-- 1-- #include