I am guessing this is a total beginners error. I have a hs-file in it I have written
let mylist = [1,2,3]
When I run it using :load in ghci I get the following error
parse error (possibly incorrect indentation)
The statement works when I am in Prelude mode. So basically I am wondering why is this not working when loading a file, and what’s the difference between Prelude and Main?
In GHCi the syntax is a little different because it acts sort of like you’re in a
do-block, so you have to use thelet x = ...form. In a Haskell source file you can just drop theletand write:As for the difference between
PreludeandMain, thePreludeis a standard module which defines the most common Haskell types and functions, and it’s imported by default into every Haskell module.Mainis just the default name for a module that does not havemodule Foo where ...at the top.When using GHCi with default settings, the prompt shows which modules are currently in scope. If you just started GHCi without loading a file, this will be just the
Prelude, so the prompt looks like this:After loading a module, the prompt changes to show the new module that was brought into scope. As mentioned previously, this will be
Mainif you didn’t give it a different name.The asterisk means that the module has been loaded in interpreted mode, which means that everything that is in scope in the module will also be in scope at the GHCi prompt, including stuff imported from other modules such as the
Prelude.You can bring additional modules into scope by using the
:mcommand. Notice how the prompt changes to show the additional module.For more information, type
:helpin GHCi or read the GHCi chapter of the GHC User’s Guide.