Running the compiled Haskell program; getting errors

Ok, so following my previous question , I got the following code:

module Main where import Data.List chain n | n == 0 = error "What are you on about?" | n == 1 = [1] | rem n 2 == 0 = n : chain (n `div` 2) | otherwise = n : chain (3 * n + 1) chainLength n = (n,length (chain n)) array = map chainLength [1..999] lengths = map chainLength [1..1000000] compareSnd (_, y1) (_, y2) = compare y1 y2 longestChain = maximumBy compareSnd lengths 

From GHCi, it loads as a module, but the start of longestChain ends with a stack overflow. The solution to this problem, which is not a complete correspondence, is to increase the size of the stack. So I am compiling with: ghc --make chain.hs

I get an error message:

 chain.hs:1:0: The function 'main' is not defined in the module 'main' 

Where do I need to put the main function for proper compilation.
Then, after compiling, how do I get to run output or use a command? I suppose that:

 ghc chain.o +RTS -K128M 

After compiling, I only need to run longestChain with a large stack size.

+4
source share
2 answers

To compile the executable in Haskell, you need to define a function called main . Something like that:

 main = print longestChain 

anywhere in the main module.

ghc --make documentation on ghc --make .

+8
source

The problem in your program is that MaximumBy seems to have an error. You must tell the GHC people about this :)

Here's the fixed version:

 maximumByFixed :: (Ord a) => (a -> a -> Ordering) -> [a] -> a maximumByFixed op (h:t) = step ht where step v [] = v step v (h:t) | v `op` h == LT = step ht | otherwise = step vt 

As for why it won't be built, you need to have a β€œmain” function, as Martigno says. However, ghci is just a GHC program, you can always run:

 ghci Main.hs +RTS -K128M 

Of course, since your program takes quite a while to run it, it is not a bad idea to compile it. You can also compile the module for use with GHCI by adding an export and changing the name from Main:

 module Chain (longestChain) where 

Then run:

 ghc -O2 --make Chain.hs 

And then run ghci as usual:

 ghci Chain.hs 

This will automatically load the compiled object if it is updated.

+2
source

All Articles