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.
source share