Search code examples
haskellcompilationprogram-entry-point

Cannot get main function to compile in Haskell


I am relatively new to Haskell and everything I have done has been completed through GHCi, however, now I am trying to compile using GHC, however, I constantly get the error message The IO action ‘main’ is not defined in module ‘Main’, I have tried declaring main = do, however, I receive more errors then, namely parse error on input ‘=’ from the line fib 0 = 1.

Here is the code I am working with:

module Main where
    fib :: Integer -> Integer
    fib 0 = 1
    fib 1 = 1
    fib x = fib (x - 1) + fib (x - 2)

Thanks for any help in advance!


Solution

  • You should write something like:

    module Main where
    
    fib :: Integer -> Integer
    fib 0 = 1
    fib 1 = 1
    fib x = fib (x - 1) + fib (x - 2)
    
    main :: IO ()
    main = do
        print $ fib 10
    

    Note: your fib function should not be indented, it should be left aligned.