Posted by: jorgedbucaran on: January 24, 2008
Haskell can be both interpreted or compiled, generally you’ll want to use an interpreter for testing but you’ll also need a compiler if you want to make a standalone executable.
There are several implementations of Haskell available and if you want more general information you can read it on Wikipedia. I will work with GHC which you can get here , if you don’t know what tar/gzip/bzip means, search for the binary packages or self-installers and don’t pretend you are geek, in other words, here is the Windows installer, that’s the one I got. The GHC stands for Glaslow Haskell Compiler and not GNU Haskell Compiler as I originally thought.
After you finish downloading and installing browse to the ghc bin directory which by default is c:\ghc\ghc-6.8.2\bin and you’ll find there the ghci.exe and ghc.exe which are the interpreter and compiler respectively. Instead of directly double clicking them (though you can do it for the interpreter and it will work great, at least for our newbie programs) open the command prompt at Windows, go to Start->Run type cmd and get comfortable with you new IDE. Don’t get fooled, there are dotNet implementations of Haskell as well as Eclipse extensions, but I won’t talk about that any time soon.
From the command prompt/line type ghci, this is the Haskell interpreter. You can also run the ghci passing the path to the (*.hs) Haskell source code file to execute. If everything goes well you’ll see:
c:\>ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
Prelude>Enter :? to bring up the help system and see all the available commands. To load a file use the :load command and specify the path to the Haskell file. To test this create a new text file and name it hh01.hs, save it on the c: drive (or whatever drive unit of yours). If you are on Windows Vista you may need to go through several dialogs in order to allow the writing on the c: bare bones unit so instead, save the file on your user folder, in my case c:\Users\jorgedbucaran, make sure to change the .txt extension to .hs. If you can’t see the file extension go here or here.
Enter this code in the created .hs file:
main = print("hello, world!") And save it. This may look familiar (or at least not weird enough) but don’t be deceived, Haskell is not a procedural/imperative/object oriented programming language like C++, Visual Basic or Java, it is a functional programming language, but we’ll get to that later. Again, please note that this program does not say more about Haskell, it is just a fair demonstration for us to get started.
Type from the command line, :load c:\\hh01.hs or :load c:\\users\\hh01.hs. If everything works well you’ll see:
[1 of 1] Compiling Main (c:hh01.hs, interpreted) OK, modules loaded: Main.*Main>Note that you need to escape the backslash (if you don’t know what escape means, then just type the \ twice). Enter main and you’ll see Haskell being polite. If you modify the code you’ll also need to reload the file into the interpreter because it won’t accept the new changes. To do this enter :reload and it’ll do the trick.
The function main is the entry point to the application. When you load the program/module you are on its context, so you can invoke whatever function is defined there, in this case we called the main function main and ran it because it was defined in the hh01.hs code. Once again, we’ll discuss later on Haskell being a functional language and what does that means. Bear with me, it’s like nothing you’ve ever seen, unless of course, you had programmed on Haskell, ML, Lisp, Scheme or Caml before. In which case you must be thinking, I can write a better tutorial about this. If you want to exit the interpreter, just type :quit. Remember to prefix the colon to all commands when running the ghci.
Back to the command line. Instead of interpreting the program, we can also compile it. That is as simple as:
c:\>ghc hh01.hs And it will generate an executable (as well as other files, on that later) which you can go ahead, double click and run. If you want These files will be created in the same folder the source file was located. There are several options available at compile time, run the compiler like ghc -? to review the help. We will study some of this options later, but for now what we care is to see an actual executable.
As some of you may not be familiar with the command line, let me shed some light on common questions you may have. First of all, how does the Windows command line knows about ghci and ghc? And what does it mean to run the compiler on the file context?
The first one is easy, when the GHC was installed, it added its path to the Path environment variable. Environment variables are global variables that the operating system is aware of and obviously able to retrieve, on Windows, whenever the GetEnviromentVariable(name, store, size) API function is called. Fine but that does not explain how the command line knows about the Haskell interpreter and compiler yet. Having the path to the Haskell installation appended to the Path variable allows applications that make use of this aware of any executables under that location. It’s like using namespace in C++\CLI or Imports in Visual Basic .net to resolve the scope of classes and namespaces. On Windows, you can look at the contents of this variable by accessing the System Properties dialog by right clicking on My Computer (just Computer on Vista), browse to the Advanced tab and click on the Environment Variables button at the bottom of the window.
So, does the command line uses this environment variable to know where to find the ghci and ghc? Yes. So what happens is that if you type the name of the executable, Windows will look for the program at the locations specified in the Path variable after searching in the current directory which has higher priority.
The second question is also an easy one. At the command line, you can navigate through directories using the cd directory command. This will change the current file context to that directory. That is like opening that folder and being in there. This also means that you don’t have to enter the full location of the executable you pass to the compiler. Windows will know where to find the ghc and will resolve the program by looking at the current file context, the current directory.
In a nutshell, we covered getting Haskell, loading programs and running them with the ghci (Haskell interpreter) and compile programs with ghc (Haskell compiler).
Finally, whether you believe my tutorial is good or not, I highly recommend the following links to tutorials, some of them I am still processing.