How to solve basic error compile in haskell

Hello everyone I just start leraning Haskell and every code I'm going to compile its giving me the same error:

<interactive>:1:1: error:
    • Variable not in scope: main
    • Perhaps you meant ‘min’ (imported from Prelude)

Just expect to learn how to compile in haskell


  • Try creating a Haskell source file named Exemplo.hs. The name should be spelled exactly like that, starting with an uppercase "E". The first line of the file should be a module statement that uses the same module name as the name of the source file:

    module Exemplo where  -- "Exemplo" here should match filename "Exemplo.hs"
    soma :: [Int] -> Int
    soma [] = 0
    soma (h:t) = h + soma t

    Now, in the interactive GHC prompt (that you get by running ghci), you can load this file (with :load Exemplo) and test your function. Using the command line, it will look like this:

    ~/src/overflow$ ghci
    GHCi, version 9.4.7: https://www.haskell.org/ghc/  :? for help
    ghci> :load Exemplo
    [1 of 1] Compiling Exemplo          ( Exemplo.hs, interpreted )
    Ok, one module loaded.
    ghci> soma [1,2,3,4,5]

    If you want to create a compiled executable, you must create a source file that defines a main function, and this function will determine what the program does. You can name the source file whatever you like, but you should NOT include a module statement. For example, create a file Programa.hs containing the lines:

    soma :: [Int] -> Int
    soma [] = 0
    soma (h:t) = h + soma t
    main :: IO ()
    main = do
      putStrLn "Enter a list of numbers in Haskell syntax (e.g., [1,2,3,4,5]):"
      numbers <- readLn
      putStrLn "The sum is:"
      print (some numbers)

    Now, you can compile this program with ghc and run the resulting executable. It will look like this:

    ~/src/overflow$ ghc Programa
    [1 of 2] Compiling Main             ( Programa.hs, Programa.o )
    [2 of 2] Linking Programa
    ~/src/overflow$ ./Programa
    Enter a list of numbers in Haskell syntax (e.g., [1,2,3,4,5]):
    [1,2,3,4,5]   <--- I entered this part
    The sum is:

    For a slightly more complete answer: GHC uses a module system where each file is a single module. A file can either start with a module XXX where statement that names the module, or you can leave out the module statement, in which case the Main module is assumed. When one module imports another module using an import statement:

    import SomeModule

    the compiler will try to find the module in the file SomeModule.hs, so it is important that the module name in the module SomeModule where statement matches the module name in the filename. Module names must start with an uppercase letter, so this means that Haskell source files that are supposed to be modules should also be named starting with an uppercase letter. Creating a module in Exemplo.hs will work, but exemplo.hs might not.

    The Main module is special. When you compile a file (or load a file with GHCi) that contains a module Main where statement in the first line or doesn't contain a module statement at all (and so defaults to being the Main module), the compiler expects it to be a standalone program instead of a regular module, and it looks for a main function. With older versions of GHC, you'll get a cryptic error message that maybe you meant min. Newer versions of GHC give a slightly better error message:

    Main.hs:1:1: error:
        The IO action ‘main’ is not defined in module ‘Main’

    So, this means:

    1. If you want to create a Haskell source file that is not a standalone program but instead contains functions that you want to test interactively with GHCi or compile into a module that can be imported by other modules, then you should name it something other than Main.hs, you should make sure the first letter of the filename is uppercase, and you should include a module module MyModule where statement at the top that matches the filename MyModule.hs.
    2. If you want to create a Haskell source file that is a standalone program, you can name it anything you want, with or without an uppercase letter. You should leave out the module statement, and you must define a main function that defines what the standalone program will actually do when it's run.