3

I'm working on an assignment in Haskell. However, the base code I am working with does not have a main function defined, and from the wording of the assignment I believe I am not expected to have to write any code outside of the solution to the problem. However, when I try to compile my code, I receive the error:

The IO function 'main' is not defined in module 'Main'

I assume this is because the function does not have a main function. However, when I try to write my own main function:

main :: IO ()
main = solve easy // easy is an array

I get the error:

Couldn't match expected type 'IO()' with actual type '[Int]'

The solve function's type is declared as follows:

solve :: [Int] -> [Int]

So it takes an array and returns an array. What am I doing wrong in writing my main function? Even when I try changing the declaration of main to things like:

main :: [Int]

or

main :: IO [Int]

I still can't get it to compile.

4
  • 2
    See here for an example of a main function in Haskell. I dispute the notion that your instructor is requiring you to complete the assignment without a main function, unless he specifically instructed you as such. Commented Apr 7, 2015 at 5:39
  • Thanks a bunch, the print statement solved our problem. Commented Apr 7, 2015 at 5:49
  • easy is not an array, it is a list. :) Commented Apr 7, 2015 at 6:18
  • main must be :: IO t, and [Int] is most definitely not IO [Int] Just say print $ at the start of the declaration. Also, does it need to be compiled? Commented Apr 7, 2015 at 15:55

4 Answers 4

9

Without writing a proper main with a correct type, as described by @G Philip, you can load your file in ghci by writing ghci file.hs in your terminal, or by invoking :l file.hs inside ghci.

Sign up to request clarification or add additional context in comments.

Comments

8

Firstly: the function main must have type IO t for some type t. When the program is executed, the computation defined by main is executed, and its result (of type t) is thrown away; see here.

So, in particular, you cannot have the type of main as [Int] and have the compiler not complain.

Depending on whether you want to see the results of solving the easy case or not, you can try one of the following:

If you want to see the results: print them!

main :: IO ()
main = putStrLn $ show (solve easy)

If you are not interested in seeing the results, throw them away:

main :: IO ()
main = let solution = solve easy 
       in putStrLn ""

Edit: Note, however, that if you do the latter, then (as @yatima2975 mentions in a comment) the "solve easy" part will not be evaluated.

3 Comments

putStrLn . show is the same as print, and the second solution only makes sure the types check; that is, solution is not evaluated at all!
@yatima2975 : True. I was not sure of your second point when I wrote the answer, so I did not mention it then. I will add it to the answer now.
If you want to evaluate the solution but not print it for whatever reason, use main = Control.Exception.evaluate (solve easy) >> return (). Note this only evaluates to whnf.
5

Just to add to the other answers:

you don't need to write a main function and can still compile the file into a lib and the easiest way to do this is just giving a module name (that is not Main):

module MyCode where

solve :: ...

But of course compiling it might not make any sense anymore (as you will not be able to run it - and of course even if you have not specified what to output anyway).

So in this case rather load the file into ghci:

ghci MyFile.hs

and then everytime you changed something in your code you can do :r inside ghci to reload it.

Or even better set up your favorite editor (emacs and vi are quite easy but sublime text and some other works great too) to give you integrated ghci - this explains what you need to do to setup emacs with haskell-mode if you are interested.

Comments

0

Consider

main :: IO ()
main = do
    let res = solve easy // easy is an array
    return ()

where return () yields a result of type Unit which conveys with the type signature of main. Note solve easy is bound to res which is not used further.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.