5

I can't figure out how to get WinGHCi to load and compile my .hs file.

I have a file, C:\Users\Haskell\Source\hello.hs, that only contains the following line:

main = putStrLn "Hello, world!"

If, at the Prelude> prompt, I run

:cd C:\Users\Haskell\Source\

nothing happens, which I'm assuming means the command was successful. However, when I try to run

:load hello.hs

I get a "[1 of 1] Compiling Main. Ok, modules loaded: Main" message. My prompt then changes from "Prelude" to "*Main" and I type:

ghc -o hello hello.hs

After that, I will get a series of errors talking about how ghc, o, hello, hello, and hs are "Not in scope."

I am in the correct directory. Why won't my program run?

One of my problems is that I'm unable to navigate the directories. I know that :!dir lists the files, and I am in the right directory, but :load hello.hs still doesn't work and I keep getting the scope error.

Any help would be appreciated.

EDIT: A user pointed out that if I have gotten to the *Main prompt, then my program has been loaded and compiled and I do not need to run the ghc command. If that is the case, how would I run it? Haskell.org states that, "You can then run the executable (./hello on Unix systems, hello.exe on Windows)," but an exe has not been created.

Joffrey Baratheon
  • 494
  • 1
  • 5
  • 19
  • 1
    I guess that GHCi successfully loaded the source file and emitted an error because there is an error in the source file. It would help if you posted hello.hs. – snak Dec 25 '14 at 03:53
  • The contents of hello.hs are, main = putStrLn "Hello, world!" – Joffrey Baratheon Dec 25 '14 at 04:01
  • 1
    @JoffreyBaratheon I've added information from your last comment into your question. In the future, click "edit" underneath your question to edit your question and add such info yourself. You'll stand a better chance of getting a good answer if you make your questions self-contained. – jub0bs Dec 25 '14 at 04:06
  • The joy of ghci is that you can use _any_ function from your file or its imports, so if you put `fibs = 1:1:zipWith (+) fibs (tail fibs)` in hello.hs, then in ghci type `take 20 fibs`, you'll see the first 20 Fibonacci numbers before your eyes. You don't have to restrict yourself to typing `main`. – AndrewC Dec 25 '14 at 09:15

1 Answers1

4

I find it easier to first navigate to the directory then invoke ghci. Once in Prelude you can use :l and the file name.

Or, you could load ghci then use :l and use the fully qualified path for the file.

Edit: After reading your edits, it is clear you are getting your code compiled fine. Once it says it has compiled, there is no reason to try and do so again with ghc (I don't think you can do that from within ghci anyhow).

Now that it is compiled, you can use any of the code and data types defined there in. So to use your main function, just type in main at the *Main> prompt.

Andrew Monshizadeh
  • 1,784
  • 11
  • 16
  • Ok, so I did what you said about navigating to the source file in cmd and then load ghci. I do: Prelude> :l hello.hs and then *Main> ghc -o hello hello.hs And I still get not in scope. – Joffrey Baratheon Dec 25 '14 at 03:59
  • When it switches the prompt to `Main` that should mean your file has loaded and compiled. So there should be no need to try and execute `ghc`. Just use the functions and data types that are defined in your file. – Andrew Monshizadeh Dec 25 '14 at 04:18
  • If you are saying that it has already compiled, how would I go about running it? Haskell.org says, "You can then run the executable (./hello on Unix systems, hello.exe on Windows)." If I type hello.exe, it doesn't work. I feel really dumb right now lol. – Joffrey Baratheon Dec 25 '14 at 04:23
  • 1
    You are using `ghci` which is the REPL. If you want an .exe you need to just use the `ghc` command. `ghci` is for interactive development while `ghc` is for normal compilation. – Andrew Monshizadeh Dec 25 '14 at 04:25
  • Thanks for the help, I REALLY appreciate it! It worked! Happy holidays to you and everyone who replied! – Joffrey Baratheon Dec 25 '14 at 04:27
  • 1
    @JoffreyBaratheon If Andrew's answer solved your problem, consider upvoting it (by clicking the up arrow on the left) and accepting it (by clicking the tick mark). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – jub0bs Dec 25 '14 at 04:39