user3754485
user3754485

Reputation:

Running a compiled Haskell program:

data Figura = Circulo Float | Rectangulo Float Float


esRedondo :: Figura -> Bool
esRedondo (Circulo _) = True
esRedondo (Rectangulo _ _) = False 

area :: Figura -> Float
area (Circulo r) = pi*r*r
area (Rectangulo h b) = h*b

I get an error : The functionmain' is not defined in module `Main'

Upvotes: 1

Views: 136

Answers (3)

mariop
mariop

Reputation: 3216

There are two reasons why this happens:

  • you haven't specified a module name in your file and when you don't specify the module then GHC assumes that you are in the Main module.
  • you haven't specified the entry point of a Haskell program, that is the main function.

The sum of this two reasons gives your error: no main function in the Main module.

One solution could be to add the main function:

main :: IO ()
main = return () -- do nothing

or, in alternative, compile your file as a library and then load it in ghci (or just load the .hs file in ghci). In this case you should give a module name to your library:

module Geometry where
[...]

and then import it where you use it with import Geometry.

Upvotes: 0

Christopher King
Christopher King

Reputation: 10941

Here is what you probably want to do if you are using ghc. Do ghci yourprogram.hs. This will allow to interact with your program interactively. Your program doesn't currently do anything by itself, so this will be more useful.

Upvotes: 2

sepp2k
sepp2k

Reputation: 370082

If you want to create a runnable executable, you need to define main :: IO (), which will be executed when the program is run.

Upvotes: 5

Related Questions