Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Source Files to Run in Haskell Programming (WinGHCi)

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.

like image 648
Joffrey Baratheon Avatar asked Sep 29 '22 03:09

Joffrey Baratheon


1 Answers

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.

like image 129
Andrew Monshizadeh Avatar answered Oct 04 '22 02:10

Andrew Monshizadeh