Ok, so following on from my previous question I have ended up with 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 the GHCi this loads fine as a module, but running longestChain ends up with a stack overflow. The resolution to this problem that isn't a complete rewrite is to increase the stack size. So I compile with: ghc --make chain.hs
I get an error:
chain.hs:1:0: The function 'main' is not defined in the module 'main'
Where do I need to put the main function in to make it compile properly.
Then once compiled, how do I make it run the output or use the command?
I assume with:
ghc chain.o +RTS -K128M
Once compiled, I only need it to run longestChain with a large stack size.