1
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'

3 Answers 3

5

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

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

2 Comments

I am beginner in Haskell, as I implemented it?
@user3754485 (Assuming you meant "how do I implement main?") That depends on what you want to happen when your program is run. A simple example would be main = print (area (Circulo 42)).
2

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.

Comments

0

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.

Comments