[Haskell-beginners] Trying to compile my first program that imports another program

Dean Herington heringtonlacey at mindspring.com
Mon Apr 26 01:06:09 EDT 2010


MAN's nutshell explanation is good, and his 
rewrite of the Main module is type-correct.  But 
note that the result of the `main` action (of 
type IO t for some type t) is deliberately 
discarded, so you probably want something more 
useful, such as:

module Main where
import Prime
main = print (primeQ 123)

Dean

At 10:25 PM -0300 4/25/10, MAN wrote:
>Hi, Mitchell
>
>First of all, you don't really need to compile your module Prime to be
>able to import it. Supposing you just want to, though:
>
>Your funcion primeO is 'pure', it work on numbers only, and will return
>the same results for the same arguments every time, without "launching
>missiles" or side-effects of any kind.
>
>The main function (in the Main module), which is the 'main entry
>point' (like in C), is of type 'IO ()' ... this means a lot, and you
>should really look into types for Haskell; but in a nutshell, it means
>the function 'main' may have side-effects (like printing to stdout, or
>opening a socket, deleting a file, etc) which cannot be predicted.
>
>Haskell is very careful as to keep pure code pure, and non-pure code,
>well, non-pure... [check out 'monads']. The 'main' function is of type
>'IO ()', so all functions called by it must have type 'IO something'.
>[IO is a monad]. Your function prime0 is of type 'Bool', so you need to
>inject it into the 'IO' [get it into the monad]. This is done with the
>function 'return' (which is quite different to that of C):
>
>module Main where
>import Prime
>main = return (primeQ 123)
>
>BTW, your Haskell program must have a Main module. You will write your
>modules with "module SomeThing where", and name that file SomeThing.hs;
>the Main module can have any filename you want, though.
>
>
>El dom, 25-04-2010 a las 21:07 -0400, Mitchell Kaplan escribió:
>>  Hi,
>>
>> 
>>
>>  I created (with help) a function to test for prime numbers.  It worked
>>  well enough for now in ghci.
>>
>> 
>>
>>  ----------------
>>
>>      f x n y
>>
>>        | n>y = True
>>
>>        | rem x n == 0 = False
>>
>>        | otherwise = f x (n+1) y
>>
>> 
>>
>>      primeQ x = f x 2 y
>>
>>        where
>>
>>        y = floor(sqrt(fromIntegral x))
>>
>>  ---------------
>>
>> 
>>
>>  I then wanted to create object code so that I could import it.  It
>>  seemed that I had to precede the above with the 2 lines:
>>
>>    
>>
>>  ----------------
>>
>>      module Prime
>>
>>      where
>>
>>  ----------------
>>
>> 
>>
>>  I ran:
>>
>>     ghc -c prime.hs, and created prime.o and prime.hi.
>>
>> 
>>
>> 
>>
>>  Next, I wanted to write a program to import and use this function.
>>
>> 
>>
>>  I wrote:
>>
>> 
>>
>>  ------------
>>
>>      module Main () where
>>
>>      import Prime
>>
>>      main = primeQ 123
>>
>>  ------------
>>
>> 
>>
>>  I tried to compile this with:
>>
>>      ghc -o test Main.hs prime.o
>>
>> 
>>
>>  I got the following error:
>>
>>      Main.hs:5:0:
>>
>>          Couldn't match expected type 'IO t' against inferred type
>>  'Bool'
>>
>>          In the expression: main
>>
>>          When checking the type of the function 'main'
>>
>>  ----------------
>>
>> 
>>
>>  First I'd like a hint as to what I need to do to make this work.
>>
>> 
>>
>>  It's pretty obvious that I don't know what I'm doing with regard to
>>  types.  Also, I have no idea if I have to name this module Main, but
>>  when I didn't the compiler complained about that.
>>
>> 
>>
>>  In the function that I think I had to re-write to make object code, I
>>  wound up with 2 where statements, which worries me.
>>
>> 
>>
>>  I'd really appreciate any help in getting me unraveled.
>>
>> 
>>
>  >         Mitchell


More information about the Beginners mailing list