Error loading .hs file using GHC - haskell

I'm a complete beginner with Haskell. So far I've downloaded the GHC. I know how to use it via the terminal by typing
$ ghci
also I have made a file called test.hs with my own code. I would like to run the file in ghci via terminal but i keep getting this error.
I have saved the test.hs on a file on my desktop. Im assuming i have to state the file path but i don't know how!.

Have you checked that you are on the correct folder before loading it and the file exists? ghci is the command line interpreter so you can load a file after calling it by typing :l test.hs .

If the file is on your desktop, use
ghci ~/Desktop/test.hs
Note that this is not really a Haskell question, but a shell question (and the path is specific to Mac OS X).

Related

<interactive>:7:36: error: parse error on input `.' in command prompt using haskell ghci when my source file only has "Hello world" in it

imge of errror
I'm very new to haskell and the entire installation process was confusing to me. I downloaded power shell and then installed chocolatey and haskell. now im putting ghci into my command prompt and trying to run my .hs file.
In order to load a file, you should work with the :load (or its shortcut :l) directive, so:
:load C:\Users\gravon\OneDrive\Desktop\yoo.hs
once you loaded the file, you can run a function, for example a main, given this function is defined in the yoo.hs.
You can also work with runhaskell [haskell.org] and thus run a file in the shell with:
$ runhaskell 'C:\Users\gravon\OneDrive\Desktop\yoo.hs'

Compiling Haskell programs in Windows: is it possible without downloading something such as Cygwin?

I am reading "Learn you a Haskell for great good", and have reached the chapter on I/O actions. I am using Windows, and have downloaded GHCi and WinGHCi.
I'm trying to compile a simple program and have saved a file on emacs as helloworld.hs with main = putStrLn "hello, world" in it.
The book says
Open your terminal, navigate to the directory where helloworld.hs is located, and enter the following:
$ ghc --make helloworld
However, if I enter $ ghc --make helloworld onto GHCi and WinGHCi I receive errors such as the one below, perhaps because I need to enter $ ghc --make helloworld elsewhere:
<interactive>:7:1: error:
parse error on input ‘$’
Perhaps you intended to use TemplateHaskell
In fact, in the online edition of the book (here: http://learnyouahaskell.com/input-and-output), the author remarks that:
For the purposes of this chapter, I'm going to assume you're using a unix-y environment for learning Haskell. If you're in Windows, I'd suggest you download Cygwin, which is a Linux-like environment for Windows, A.K.A. just what you need.
Question 1: Is it necessary for me to download Cygwin, or some other such thing, in order to be able to compile the program by entering $ ghc --make helloworld?
I ask this, since if there were some other way of compiling the program it would be very useful to know, since I am on a university computer and cannot download things like Cygwin without permission. (and even with permission it might not be possible, depending on what Cygwin requires)
Question 2: Given that only the technicians of my university are away for Easter, and only they can download things on this computer, how can I proceed with the chapter in I/O actions? Is it possible just to continue, but ignore the sections involving compiling programs?
In many Unix and Linux shells (at least in Bash with which I'm most familiar) the default prompt is $. That's what the shell displays.
On Windows, when you open a Command Prompt, you'll typically see something like this instead:
C:\Users\mark>
(That's my prompt.)
While instructions often include the $ prompt in order to indicate that the code is something that you're expected to enter at a command prompt, you're not expected to type the $. Just type ghc --make helloworld and hit Enter.
Also, you're not expected to do this from within GHCi, but from the command line. ghc is a command-line executable (an .exe), just like e.g. git. It has to be in your path, though, or otherwise, you'll have to type the full path the executable every time you want to run it.
You have downloaded ghci, which is interpreter. It is, by my opinion, totally enough to learn with it (it can :load sources as well), but you need to run a bit other commands. I don't know if your installation include compiler, I would not assume anything about it.
btw, you are typing command inside the interpreter already, the error message is specific for that
ps: to do same as "ghc --make" from the interpreter, run:
:load <file>
Yes, it will recurse to depenzecies automatically. You could use :help to learn other commands

Windows: Loading Haskell-Source-Code into ghci

I am a newbie to Haskell and my problem is probably very trivial, yet I do not know how to solve it.
I created a minimal source code (for faculty, as in the tutorial) with the Editor in Windows 7, then saved it as "test.hs" . Now if I try to load the file into the Haskell interpreter (ghci), then it will give me the following error message:
: can't find file S:\Haskell-Code\test.hs
Failed. modules loaded: none.
I am sure that I am in the right directory, I also tried loading test (which gives me another error message telling me that it isn't a module), also renamed the file to just "test". Is there something I am missing?
Thanks in advance.
Can you start ghci (in the right directory) and then type
:!dir
Does it display the right directory?
Do you see the file test.hs?

How can I load a runhaskell script without a .hs extension with ghci?

I have written a script in haskell named testscript with the following code:
#!/usr/bin/env runhaskell
main = putStrLn "hello"
After making the script executable, I can run it using ./testscript. However, when I try to load the script with ghci (ie with :l testscript), I get the error
target `testscript' is not a module name or a source file
If I rename testscript to testscript.hs, and try loading with ghci again, I get the error
testscript.hs:1:0: error: invalid preprocessing directive #!
phase `C pre-processor' failed (exitcode = 1)
If I remove the shebang line it works fine. However it is tedious to have to add a .hs extension to the script, remove the top line, then remove the .hs extension and add the shebang line every time I want to try the script in ghci (which is pretty common everytime I want to make a change to it). Is there an easier way to do this?
I'm using ghc version 7.0.3 under Mac OS X 10.6.8
You can use the -x option to tell GHCi (or GHC for that matter) to treat all following files as if they had the specified extension.
There doesn't seem to be any way to specify this option from within GHCi (for use with :load), but a workaround you can use if you want this is to create a symlink with a .hs extension and load that.
Your second problem with the shebang line is caused by the C preprocessor being run on your source file for some reason (my old GHC install on Ubuntu does not do this). You can disable this by using the -XNoCPP option.
So for your case, this should work from the command line:
ghci -x hs -XNoCPP testscript

What directory do you put .hs files in so you can load them using :l in GHCI?

I am running GHCI 6.4 in Windows 7. I do not know what sub directory of the .\ghc\ directory to add .hs files to. Every time I try using :l it tells me that the file cannot be found. Has Anybody else run into this problem?
EDIT: Thank you for your answers. Both of those look like they will work, but I ended up using the ":cd " command as per my professor's recommendation.
You can add a directory PATH to the search path with :set -iPATH. After that, you can load files that are in that directory.
I've only used GHCi a little, but I've always been able to do :l D:\Documents\Haskell\myscript.hs with no problems.

Resources